package main
import (
"fmt"
"github.com/go-ini/ini"
"os"
)
func main() {
cfg, err := ini.Load("src/test/my.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
// 典型读取操作,默认分区可以使用空字符串表示
// cfg.Section("").Key("app_mode").String()
// cfg.Section("paths").Key("data").String()
// 输出默认分区所有对应的key和value
// key := cfg.Section("").KeyStrings()
// value := cfg.Section("").Keys()
// 如果读取的值不在候选列表内,则会回退使用提供的默认值
// cfg.Section("server").Key("protocol").In("http", []string{"http", "https"})
// 自动类型转换
// fmt.Printf("Port Number: (%[1]T) %[1]d\n", cfg.Section("server").Key("http_port").MustInt(9999))
// fmt.Printf("Enforce Domain: (%[1]T) %[1]v\n", cfg.Section("server").Key("enforce_domain").MustBool(false))
// 获取所有分区对象
// sections := cfg.Sections()
// 获取所有分区名称
// names := cfg.SectionStrings()
// 差不多了,修改某个值然后进行保存
// cfg.Section("").Key("app_mode").SetValue("production")
// cfg.SaveTo("my.ini.local")
}
参考:https://ini.unknwon.io/docs/intro/getting_started
Comments Closed.