如果遇到一些比较特殊的分区,它们不包含常见的键值对,而是没有固定格式的纯文本,则可以使用 LoadOptions.UnparsableSections 进行处理,这中间还有两个前置条件记得补上。踩了半天的坑才发现的诀窍。。。
有UnpareableSections 还是会报错,还需要增加
SkipUnrecognizableLines true 是否跳过不符合键/值对的无法识别的行
Loose true 忽略不存在的文件或返回错误
然后使用.Section.Body() 获取 string
cfg, err := ini.LoadSources(ini.LoadOptions{SkipUnrecognizableLines: true, Loose: true, UnparseableSections: []string{"Mount"}}, fileName)
note := cfg.Section("Mount").Body()
文章评论