INI
INI data parse by golang. INI config data management tool library.
中文说明
Features
- Easy to use(get:
Int
Int64
Bool
String
StringMap
..., set: Set
) - Support multi file, data load
- Support data override merge
- Support parse ENV variable
- Complete unit test(coverage > 90%)
- Support variable reference, default compatible with Python's configParser format
%(VAR)s
More formats
If you want more support for file content formats, recommended use gookit/config
- gookit/config - Support multi formats:
JSON
(default), INI
, YAML
, TOML
, HCL
GoDoc
Install
go get github.com/gookit/ini/v2
Usage
- example data(
testdata/test.ini
):
name = inhere
age = 50
debug = true
hasQuota1 = 'this is val'
hasQuota2 = "this is val1"
can2arr = val0,val1,val2
shell = ${SHELL}
noEnv = ${NotExist|defValue}
nkey = val in default section
[sec1]
key = val0
some = value
stuff = things
varRef = %(nkey)s
Load data
package main
import (
"github.com/gookit/ini/v2"
)
func main() {
err := ini.LoadExists("testdata/test.ini", "not-exist.ini")
if err != nil {
panic(err)
}
err = ini.LoadStrings(`
age = 100
[sec1]
newK = newVal
some = change val
`)
}
Read data
age := ini.Int("age")
fmt.Print(age)
val := ini.Bool("debug")
fmt.Print(val)
name := ini.String("name")
fmt.Print(name)
- Get section data(string map)
val := ini.StringMap("sec1")
fmt.Println(val)
value := ini.String("shell")
fmt.Printf("%q", value)
value := ini.String("sec1.key")
fmt.Print(value)
value := ini.String("sec1.varRef")
fmt.Printf("%q", value)
ini.Set("name", "new name")
name = ini.String("name")
fmt.Printf("%q", value)
Variable reference resolution
[portal]
url = http://%(host)s:%(port)s/api
host = localhost
port = 8080
If variable resolution is enabled,will parse %(host)s
and replace it:
cfg := ini.New()
cfg.WithOptions(ini.ParseVar)
fmt.Print(cfg.MustString("portal.url"))
Available options
type Options struct {
Readonly bool
ParseEnv bool
ParseVar bool
VarOpen string
VarClose string
IgnoreCase bool
DefSection string
SectionSep string
}
- setting options for default instance
ini.WithOptions(ini.ParseEnv,ini.ParseVar)
- setting options with new instance
cfg := ini.New()
cfg.WithOptions(ini.ParseEnv,ini.ParseVar, func (opts *Options) {
opts.SectionSep = ":"
opts.DefSection = "default"
})
Tests
go test ./... -cover
golint ./...
Refer
License
MIT