Home

Awesome

INI

GitHub go.mod Go version GitHub tag (latest SemVer) Coverage Status Go Report Card Unit-Tests Go Reference

INI contents parser by Golang, INI config data management.

中文说明

Features

Parser

Package parser is a Parser for parse INI format content to golang data

Dotenv

Package dotenv that supports importing ENV data from files (eg .env)

More formats

If you want more support for file content formats, recommended use gookit/config

GoDoc

Install

go get github.com/gookit/ini/v2

Usage

# comments
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

; comments
[sec1]
key = val0
some = value
stuff = things
varRef = %(nkey)s

Load data

package main

import (
	"github.com/gookit/ini/v2"
)

// go run ./examples/demo.go
func main() {
	// config, err := ini.LoadFiles("testdata/tesdt.ini")
	// LoadExists will ignore not exists file
	err := ini.LoadExists("testdata/test.ini", "not-exist.ini")
	if err != nil {
		panic(err)
	}

	// load more, will override prev data by key
	err = ini.LoadStrings(`
age = 100
[sec1]
newK = newVal
some = change val
`)
	// fmt.Printf("%v\n", config.Data())
}

Read data

age := ini.Int("age")
fmt.Print(age) // 100
val := ini.Bool("debug")
fmt.Print(val) // true
name := ini.String("name")
fmt.Print(name) // inhere
val := ini.StringMap("sec1")
fmt.Println(val) 
// map[string]string{"key":"val0", "some":"change val", "stuff":"things", "newK":"newVal"}
value := ini.String("shell")
fmt.Printf("%q", value)  // "/bin/zsh"
value := ini.String("sec1.key")
fmt.Print(value) // val0
value := ini.String("sec1.varRef")
fmt.Printf("%q", value) // "val in default section"
// set value
ini.Set("name", "new name")
name = ini.String("name")
fmt.Printf("%q", name) // "new name"

Mapping data to struct

type User struct {
	Name string
	Age int
}

user := &User{}
ini.MapStruct(ini.DefSection(), user)

dump.P(user)

Special, mapping all data:

ini.MapStruct("", ptr)

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()
// enable ParseVar
cfg.WithOptions(ini.ParseVar)

fmt.Print(cfg.MustString("portal.url"))
// OUT: 
// http://localhost:8080/api 

Available options

type Options struct {
	// set to read-only mode. default False
	Readonly bool
	// parse ENV var name. default True
	ParseEnv bool
	// parse variable reference "%(varName)s". default False
	ParseVar bool

	// var left open char. default "%("
	VarOpen string
	// var right close char. default ")s"
	VarClose string

	// ignore key name case. default False
	IgnoreCase bool
	// default section name. default "__default"
	DefSection string
	// sep char for split key path. default ".", use like "section.subKey"
	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"
})

Dotenv

Package dotenv that supports importing data from files (eg .env) to ENV

Usage

err := dotenv.Load("./", ".env")
// err := dotenv.LoadExists("./", ".env")

val := dotenv.Get("ENV_KEY")
// Or use 
// val := os.Getenv("ENV_KEY")

// get int value
intVal := dotenv.Int("LOG_LEVEL")

// with default value
val := dotenv.Get("ENV_KEY", "default value")

Tests

go test ./... -cover
golint ./...

Gookit packages

Related

License

MIT