per = make(map[string]interface{})
f, _ := os.Open("D:\\GoPoject\\initype\\src\\ini\\gconfig.ini")
buf := bufio.NewReader(f)
for {
l, err := buf.ReadString('\n')
line := strings.TrimSpace(l)
if err != nil {
if err != io.EOF {
}
if len(line) == 0 {
break
}
}
switch {
case len(line) == 0:
case line[0] == '[' && line[len(line)-1] == ']':
section := strings.TrimSpace(line[1 : len(line)-1])
fmt.Println(section)
default:
i := strings.IndexAny(line, "=")
per[strings.TrimSpace(line[0:i])] = strings.TrimSpace(line[i+1:])
}
}
for k, v := range per {
fmt.Println(k, v)
}
报错panic: runtime error: slice bounds out of range
看你的具体需求,读取配置时发现配置写法错误应该怎么处理。具体来说,就是当配置不符合你定义的 key = value 模式时,你可以主动提示错误:
```
i := strings.IndexAny(line, "=")
if i < 0 {
panic(fmt.Sprintf(" %s can't coincidence key = value pattern", line))
}
key := strings.TrimSpace(line[0:i])
if key == "" {
panic(fmt.Sprintf(" %s key is empty", line))
}
value := strings.TrimSpace(line[i+1:])
if value == "" {
panic(fmt.Sprintf(" %s value is empty", line))
}
per[key] = value
```
具体怎么写由你决定
#10
更多评论