README: update docs for ShadowLoad (#136)

This commit is contained in:
Unknwon
2018-03-06 06:20:24 -05:00
parent 1d1add4bb9
commit a76e4b8c9a
2 changed files with 54 additions and 0 deletions
+27
View File
@@ -489,6 +489,33 @@ cfg.Section("package.sub").Key("CLONE_URL").String() // https://gopkg.in/ini.v1
cfg.Section("package.sub").ParentKeys() // ["CLONE_URL"]
```
### Same Key with Multiple Values
Do you ever have a configuration file like this?
```ini
[remote "origin"]
url = https://github.com/Antergone/test1.git
url = https://github.com/Antergone/test2.git
fetch = +refs/heads/*:refs/remotes/origin/*
```
By default, only the last read value will be kept for the key `url`. If you want to keep all copies of value of this key, you can use `ShadowLoad` to achieve it:
```go
cfg, err := ini.ShadowLoad(".gitconfig")
// ...
f.Section(`remote "origin"`).Key("url").String()
// Result: https://github.com/Antergone/test1.git
f.Section(`remote "origin"`).Key("url").ValueWithShadows()
// Result: []string{
// "https://github.com/Antergone/test1.git",
// "https://github.com/Antergone/test2.git",
// }
```
### Unparseable Sections
Sometimes, you have sections that do not contain key-value pairs but raw content, to handle such case, you can use `LoadOptions.UnparsableSections`:
+27
View File
@@ -482,6 +482,33 @@ cfg.Section("package.sub").Key("CLONE_URL").String() // https://gopkg.in/ini.v1
cfg.Section("package.sub").ParentKeys() // ["CLONE_URL"]
```
### 同个键名包含多个值
你是否也曾被下面的配置文件所困扰?
```ini
[remote "origin"]
url = https://github.com/Antergone/test1.git
url = https://github.com/Antergone/test2.git
fetch = +refs/heads/*:refs/remotes/origin/*
```
没错!默认情况下,只有最后一次出现的值会被保存到 `url` 中,可我就是想要保留所有的值怎么办啊?不要紧,用 `ShadowLoad` 轻松解决你的烦恼:
```go
cfg, err := ini.ShadowLoad(".gitconfig")
// ...
f.Section(`remote "origin"`).Key("url").String()
// Result: https://github.com/Antergone/test1.git
f.Section(`remote "origin"`).Key("url").ValueWithShadows()
// Result: []string{
// "https://github.com/Antergone/test1.git",
// "https://github.com/Antergone/test2.git",
// }
```
### 无法解析的分区
如果遇到一些比较特殊的分区,它们不包含常见的键值对,而是没有固定格式的纯文本,则可以使用 `LoadOptions.UnparsableSections` 进行处理: