funcmain() { cfg, err := ini.Load("my.ini") if err != nil { fmt.Printf("Fail to read file: %v", err) os.Exit(1) }
// Classic read of values, default section can be represented as empty string fmt.Println("App Mode:", cfg.Section("").Key("app_mode").String()) fmt.Println("Data Path:", cfg.Section("paths").Key("data").String())
// Let's do some candidate value limitation fmt.Println("Server Protocol:", cfg.Section("server").Key("protocol").In("http", []string{"http", "https"})) // Value read that is not in candidates will be discarded and fall back to given default value fmt.Println("Email Protocol:", cfg.Section("server").Key("protocol").In("smtp", []string{"imap", "smtp"}))
// Try out auto-type conversion 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)) // Now, make some changes and save it cfg.Section("").Key("app_mode").SetValue("production") cfg.SaveTo("my.ini.local") }
Almost there, let’s run this program and check the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ go run main.go App Mode: development Data Path: /home/git/grafana Server Protocol: http Email Protocol: smtp Port Number: (int) 9999 Enforce Domain: (bool) true $ cat my.ini.local # possible values : production, development app_mode = production
[paths] # Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used) data = /home/git/grafana ...
Perfect! Though the example is very basic and covered only a small bit of all functionality, but it’s a good start.
Map between struct and section
Map To Struct
Want more objective way to play with INI? Cool.
1 2 3 4 5 6 7 8
Name = Unknwon age = 21 Male = true Born = 1993-01-01T20:17:05Z
[Note] Content = Hi is a good man! Cities = HangZhou, Boston
type Author struct { Name string`ini:"NAME"` Male bool Age int`comment:"Author's age"` GPA float64 NeverMind string`ini:"-"` *Embeded `comment:"Embeded section"` }