gopkg.in/ini.v1
Configuration loaded
Create an empty configuration
cfg: = ini.Empty ()
Direct load the configuration file exists, the error will be if the file does not exist
cfg, err := ini.Load("app.ini")
A plurality of configuration files may be loaded at the same time, the following configuration files overwrites a preceding key
cfg, err := ini.Load("app.ini", "app_dev.ini")
Can not determine which file does not exist, you can ignore them by calling the function LooseLoad ().
cfg, err := ini.LooseLoad("app.ini", "app_dev.ini")
Unrecognized rows skipped
cfg, err = ini.LoadSources (ini.LoadOptions {SkipUnrecognizableLines: true}, "other.ini")
Save Configuration
More primitive approach is to configure the output to a file:
err = cfg.SaveTo("app.ini")
Adjust the indent save
err = cfg.SaveToIndent("app.ini", "\t")
Operating partition
Gets the object to develop partitions
sec, err := cfg.GetSection("db")
If you want to get the default partition, you can replace with an empty string partition name:
sec, err := cfg.GetSection("")
Correspondingly, it can also be used to get the default partition ini.DEFAULT_SECTION:
sec, err := cfg.GetSection(ini.DEFAULT_SECTION)
When you are pretty sure a partition exists, you can use the following simple method:
sec := cfg.Section("section name")
如果不存再,会自动创建并返回一个对应的分区对象。
创建一个分区:
err := cfg.NewSection("new section")
获取所有分区对象或名称:
secs := cfg.Sections()
names := cfg.SectionStrings()
操作键
key, err := cfg.Section("").GetKey("key name")
和分区一样,您也可以直接获取键而忽略错误处理:
key := cfg.Section("").Key("key name")
判断某个键是否存在:
yes := cfg.Section("").HasKey("key name")
创建一个新的键:
err := cfg.Section("").NewKey("name", "value")
获取分区下的所有键或键名:
keys := cfg.Section("").Keys()
names := cfg.Section("").KeyStrings()
获取分区下的所有键值对的克隆:
hash := cfg.Section("").KeysHash()
操作键值
获取一个类型为字符串(string)的值:
val := cfg.Section("").Key("key name").String()
获取值的同时通过自定义函数进行处理验证:
val := cfg.Section("").Key("key name").Validate(func(in string) string {
if len(in) == 0 {
return "default"
}
return in
})
如果不需要任何对值的自动转变功能(例如递归读取),可以直接获取原值(这种方式性能最佳):
val := cfg.Section("").Key("key name").Value()
判断某个原值是否存在:
yes := cfg.Section("").HasValue("test value")
获取其它类型的值:
// 布尔值的规则:
// true 当值为:1, t, T, TRUE, true, True, YES, yes, Yes, y, ON, on, On
// false 当值为:0, f, F, FALSE, false, False, NO, no, No, n, OFF, off, Off
v, err = cfg.Section("").Key("BOOL").Bool()
v, err = cfg.Section("").Key("FLOAT64").Float64()
v, err = cfg.Section("").Key("INT").Int()
v, err = cfg.Section("").Key("INT64").Int64()
v, err = cfg.Section("").Key("UINT").Uint()
v, err = cfg.Section("").Key("UINT64").Uint64()
v, err = cfg.Section("").Key("TIME").TimeFormat(time.RFC3339)
v, err = cfg.Section("").Key("TIME").Time() // RFC3339
v = cfg.Section("").Key("BOOL").MustBool()
v = cfg.Section("").Key("FLOAT64").MustFloat64()
v = cfg.Section("").Key("INT").MustInt()
v = cfg.Section("").Key("INT64").MustInt64()
v = cfg.Section("").Key("UINT").MustUint()
v = cfg.Section("").Key("UINT64").MustUint64()
v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339)
v = cfg.Section("").Key("TIME").MustTime() // RFC3339
// 由 Must 开头的方法名允许接收一个相同类型的参数来作为默认值,
// 当键不存在或者转换失败时,则会直接返回该默认值。
// 但是,MustString 方法必须传递一个默认值。
v = cfg.Section("").Key("String").MustString("default")
v = cfg.Section("").Key("BOOL").MustBool(true)
v = cfg.Section("").Key("FLOAT64").MustFloat64(1.25)
v = cfg.Section("").Key("INT").MustInt(10)
v = cfg.Section("").Key("INT64").MustInt64(99)
v = cfg.Section("").Key("UINT").MustUint(3)
v = cfg.Section("").Key("UINT64").MustUint64(6)
v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339, time.Now())
v = cfg.Section("").Key("TIME").MustTime(time.Now()) // RFC3339
Action Notes
The content of the following situations will be treated as a comment:
All with # or; at the beginning of the line
All # or; after the contents
Text after the partition label (ie content [partition name] after)
If you want to use or # include; the values, use the `or '' 'be covered.
In addition, it can also completely ignored within LoadOptions line comments:
cfg, err := ini.LoadSources(ini.LoadOptions{
IgnoreInlineComment: true,
}, "app.ini")
Or before the comment symbol must be requested with a space:
cfg, err := ini.LoadSources(ini.LoadOptions{
SpaceBeforeInlineComment: true,
}, "app.ini")