Documentation ¶
Overview ¶
Package ini is a ini config file/data manage implement
Source code and other details for the project are available at GitHub:
https://github.com/gookit/ini
INI parser is: https://github.com/gookit/ini/parser
Example ¶
// config, err := LoadFiles("testdata/tesdt.ini") // LoadExists will ignore not exists file config, err := LoadExists("testdata/test.ini", "not-exist.ini") if err != nil { panic(err) } // load more, will override prev data by key _ = config.LoadStrings(` age = 100 [sec1] newK = newVal some = change val `) // fmt.Printf("%v\n", config.Data()) iv, ok := config.Int("age") fmt.Printf("get int\n - ok: %v, val: %v\n", ok, iv) bv, ok := config.Bool("debug") fmt.Printf("get bool\n - ok: %v, val: %v\n", ok, bv) name, ok := config.String("name") fmt.Printf("get string\n - ok: %v, val: %v\n", ok, name) sec1, ok := config.StringMap("sec1") fmt.Printf("get section\n - ok: %v, val: %#v\n", ok, sec1) str, ok := config.String("sec1.key") fmt.Printf("get sub-value by path 'section.key'\n - ok: %v, val: %s\n", ok, str) // can parse env name(ParseEnv: true) fmt.Printf("get env 'envKey' val: %s\n", config.MustString("shell")) fmt.Printf("get env 'envKey1' val: %s\n", config.MustString("noEnv")) // set value _ = config.Set("name", "new name") name, ok = config.String("name") fmt.Printf("set string\n - ok: %v, val: %v\n", ok, name) // export data to file // _, err = config.WriteToFile("testdata/export.ini") // if err != nil { // panic(err) // } // Out: // get int // - ok: true, val: 100 // get bool // - ok: true, val: true // get string // - ok: true, val: inhere // get section // - ok: true, val: map[string]string{"stuff":"things", "newK":"newVal", "key":"val0", "some":"change val"} // get sub-value by path 'section.key' // - ok: true, val: val0 // get env 'envKey' val: /bin/zsh // get env 'envKey1' val: defValue // set string // - ok: true, val: new name
Output:
Index ¶
- Constants
- func IgnoreCase(opts *Options)
- func ParseEnv(opts *Options)
- func ParseVar(opts *Options)
- func Readonly(opts *Options)
- type Ini
- func (c *Ini) Bool(key string) (value bool, ok bool)
- func (c *Ini) Data() map[string]Section
- func (c *Ini) DefBool(key string, def bool) bool
- func (c *Ini) DefInt(key string, def int) (val int)
- func (c *Ini) DefSection() string
- func (c *Ini) DefString(key string, def string) string
- func (c *Ini) DelSection(name string) (ok bool)
- func (c *Ini) Delete(key string) (ok bool)
- func (c *Ini) Get(key string) (val string, ok bool)
- func (c *Ini) HasKey(key string) (ok bool)
- func (c *Ini) HasSection(name string) bool
- func (c *Ini) Int(key string) (val int, ok bool)
- func (c *Ini) IsEmpty() bool
- func (c *Ini) LoadData(data map[string]Section) (err error)
- func (c *Ini) LoadExists(files ...string) (err error)
- func (c *Ini) LoadFiles(files ...string) (err error)
- func (c *Ini) LoadStrings(strings ...string) (err error)
- func (c *Ini) MustBool(key string) bool
- func (c *Ini) MustInt(key string) int
- func (c *Ini) MustMap(name string) map[string]string
- func (c *Ini) MustString(key string) string
- func (c *Ini) NewSection(name string, values map[string]string) (err error)
- func (c *Ini) Options() *Options
- func (c *Ini) PrettyJSON() string
- func (c *Ini) Reset()
- func (c *Ini) Section(name string) (Section, bool)
- func (c *Ini) Set(key string, val interface{}, section ...string) (err error)
- func (c *Ini) SetBool(key string, value bool, section ...string) error
- func (c *Ini) SetInt(key string, value int, section ...string) error
- func (c *Ini) SetSection(name string, values map[string]string) (err error)
- func (c *Ini) SetString(key, val string, section ...string) error
- func (c *Ini) String(key string) (val string, ok bool)
- func (c *Ini) StringMap(name string) (mp map[string]string, ok bool)
- func (c *Ini) Strings(key, sep string) (ss []string, ok bool)
- func (c *Ini) WithOptions(opts ...func(*Options))
- func (c *Ini) WriteTo(out io.Writer) (n int64, err error)
- func (c *Ini) WriteToFile(file string) (int64, error)
- type Options
- type Section
Examples ¶
Constants ¶
const ( SepSection = "." DefSection = "__default" )
some default constants
Variables ¶
This section is empty.
Functions ¶
func IgnoreCase ¶ added in v1.0.1
func IgnoreCase(opts *Options)
IgnoreCase for get/set value by key
func ParseEnv ¶ added in v1.0.1
func ParseEnv(opts *Options)
ParseEnv on get value usage: ini.WithOptions(ini.ParseEnv)
Types ¶
type Ini ¶
type Ini struct {
// contains filtered or unexported fields
}
Ini config data manager
func LoadExists ¶
LoadExists load files, will ignore not exists
func LoadStrings ¶ added in v1.0.1
LoadStrings load data from strings
func NewWithOptions ¶
NewWithOptions new a instance and with some options Usage: ini.NewWithOptions(ini.ParseEnv, ini.Readonly)
func (*Ini) Bool ¶ added in v1.0.4
Bool Looks up a value for a key in this section and attempts to parse that value as a boolean, along with a boolean result similar to a map lookup. of following(case insensitive):
- true
- false
- yes
- no
- off
- on
- 0
- 1
The `ok` boolean will be false in the event that the value could not be parsed as a bool
func (*Ini) DefSection ¶ added in v1.0.5
DefSection get default section name
func (*Ini) DelSection ¶ added in v1.0.2
DelSection del section by name
func (*Ini) Get ¶
Get a value by key string. you can use '.' split for get value in a special section
func (*Ini) LoadExists ¶
LoadExists load files, will ignore not exists
func (*Ini) LoadStrings ¶
LoadStrings load data from strings
func (*Ini) MustString ¶
MustString get a string value, if not found return empty string
func (*Ini) NewSection ¶ added in v1.0.4
NewSection add new section data, existed will be replace
func (*Ini) PrettyJSON ¶ added in v1.0.6
PrettyJSON translate to pretty JSON string
func (*Ini) Set ¶
Set a value to the section by key. if section is empty, will set to default section
func (*Ini) SetSection ¶
SetSection if not exist, add new section. If exist, will merge to old section.
func (*Ini) WithOptions ¶ added in v1.0.1
WithOptions apply some options
type 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", it's allow empty string. DefSection string // sep char for split key path. default ".", use like "section.subKey" SectionSep string }
Options for config
Directories ¶
Path | Synopsis |
---|---|
Package parser is a Parser for parse INI format content to golang data There are example data: # comments name = inhere age = 28 debug = true hasQuota1 = 'this is val' hasQuota2 = "this is val1" shell = ${SHELL} noEnv = ${NotExist|defValue} ; array in def section tags[] = a tags[] = b tags[] = c ; comments [sec1] key = val0 some = value stuff = things ; array in section types[] = x types[] = y how to use, please see examples:
|
Package parser is a Parser for parse INI format content to golang data There are example data: # comments name = inhere age = 28 debug = true hasQuota1 = 'this is val' hasQuota2 = "this is val1" shell = ${SHELL} noEnv = ${NotExist|defValue} ; array in def section tags[] = a tags[] = b tags[] = c ; comments [sec1] key = val0 some = value stuff = things ; array in section types[] = x types[] = y how to use, please see examples: |