Working with keys

To get a key under a section:

  1. key, err := cfg.Section("").GetKey("key name")

Same rule applies to key operations:

  1. key := cfg.Section("").Key("key name")

To check if a key exists:

  1. yes := cfg.Section("").HasKey("key name")

To create a new key:

  1. err := cfg.Section("").NewKey("name", "value")

To get a list of keys or key names:

  1. keys := cfg.Section("").Keys()
  2. names := cfg.Section("").KeyStrings()

To get a clone hash of keys and corresponding values:

  1. hash := cfg.Section("").KeysHash()

Ignore cases of key name

When you do not care about cases of section and key names, you can use InsensitiveLoad to force all names to be lowercased while parsing.

  1. cfg, err := ini.InsensitiveLoad("filename")
  2. //...
  3. // sec1 and sec2 are the exactly same section object
  4. sec1, err := cfg.GetSection("Section")
  5. sec2, err := cfg.GetSection("SecTIOn")
  6. // key1 and key2 are the exactly same key object
  7. key1, err := sec1.GetKey("Key")
  8. key2, err := sec2.GetKey("KeY")

MySQL-like boolean key

MySQL’s configuration allows a key without value as follows:

  1. [mysqld]
  2. ...
  3. skip-host-cache
  4. skip-name-resolve

By default, this is considered as missing value. But if you know you’re going to deal with those cases, you can assign advanced load options:

  1. cfg, err := ini.LoadSources(ini.LoadOptions{
  2. AllowBooleanKeys: true,
  3. }, "my.cnf")

The value of those keys are always true, and when you save to a file, it will keep in the same foramt as you read.

To generate such keys in your program, you could use NewBooleanKey:

  1. key, err := sec.NewBooleanKey("skip-host-cache")

Same Key with Multiple Values

Do you ever have a configuration file like this?

  1. [remote "origin"]
  2. url = https://github.com/Antergone/test1.git
  3. url = https://github.com/Antergone/test2.git
  4. 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:

  1. cfg, err := ini.ShadowLoad(".gitconfig")
  2. // ...
  3. f.Section(`remote "origin"`).Key("url").String()
  4. // Result: https://github.com/Antergone/test1.git
  5. f.Section(`remote "origin"`).Key("url").ValueWithShadows()
  6. // Result: []string{
  7. // "https://github.com/Antergone/test1.git",
  8. // "https://github.com/Antergone/test2.git",
  9. // }

Auto-increment Key Names

If key name is - in data source, then it would be seen as special syntax for auto-increment key name start from 1, and every section is independent on counter.

  1. [features]
  2. -: Support read/write comments of keys and sections
  3. -: Support auto-increment of key names
  4. -: Support load multiple files to overwrite key values
  1. cfg.Section("features").KeyStrings() // []{"#1", "#2", "#3"}

Retrieve parent keys available to a child section

  1. cfg.Section("package.sub").ParentKeys() // ["CLONE_URL"]

原文: https://ini.unknwon.io/docs/howto/work_with_keys