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 err := ini.LoadExists("testdata/test.ini", "not-exist.ini") if err != nil { panic(err) } config := ini.Default() // 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 := config.Int("age") fmt.Printf("get int\n - val: %v\n", iv) bv := config.Bool("debug") fmt.Printf("get bool\n - val: %v\n", bv) name := config.String("name") fmt.Printf("get string\n - val: %v\n", name) sec1 := config.StringMap("sec1") fmt.Printf("get section\n - val: %#v\n", sec1) str := config.String("sec1.key") fmt.Printf("get sub-value by path 'section.key'\n - val: %s\n", str) // can parse env name(ParseEnv: true) fmt.Printf("get env 'envKey' val: %s\n", config.String("shell")) fmt.Printf("get env 'envKey1' val: %s\n", config.String("noEnv")) // set value _ = config.Set("name", "new name") name = config.String("name") fmt.Printf("set string\n - val: %v\n", name) // export data to file // _, err = config.WriteToFile("testdata/export.ini") // if err != nil { // panic(err) // } // Out: // get int // - val: 100 // get bool // - val: true // get string // - val: inhere // get section // - val: map[string]string{"stuff":"things", "newK":"newVal", "key":"val0", "some":"change val"} // get sub-value by path 'section.key' // - val: val0 // get env 'envKey' val: /bin/zsh // get env 'envKey1' val: defValue // set string // - val: new name
Output:
Index ¶
- Constants
- func Bool(key string, defVal ...bool) bool
- func Data() map[string]Section
- func DefSection() string
- func Delete(key string) bool
- func Error() error
- func Get(key string, defVal ...string) string
- func GetValue(key string) (string, bool)
- func HasKey(key string) bool
- func IgnoreCase(opts *Options)
- func Int(key string, defVal ...int) int
- func Int64(key string, defVal ...int64) int64
- func IsEmpty() bool
- func LoadData(data map[string]Section) error
- func LoadExists(files ...string) error
- func LoadFiles(files ...string) error
- func LoadStrings(strings ...string) error
- func MapStruct(key string, ptr any) error
- func ParseEnv(opts *Options)
- func ParseVar(opts *Options)
- func Readonly(opts *Options)
- func ReplaceNl(opts *Options)
- func Reset()
- func ResetStd()
- func SectionKeys(withDefSection bool) (ls []string)
- func Set(key string, val any, section ...string) error
- func String(key string, defVal ...string) string
- func StringMap(name string) map[string]string
- func Strings(key string, sep ...string) []string
- func Uint(key string, defVal ...uint) uint
- func WithOptions(opts ...func(*Options))
- type Ini
- func (c *Ini) Bool(key string, defVal ...bool) (value bool)
- func (c *Ini) Data() map[string]Section
- func (c *Ini) Decode(ptr any) error
- func (c *Ini) DefSection() string
- func (c *Ini) DelSection(name string) (ok bool)
- func (c *Ini) Delete(key string) (ok bool)
- func (c *Ini) Error() error
- func (c *Ini) Get(key string, defVal ...string) string
- func (c *Ini) GetValue(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, defVal ...int) (value int)
- func (c *Ini) Int64(key string, defVal ...int64) (value int64)
- 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) MapStruct(key string, ptr any) error
- func (c *Ini) MapTo(ptr any) errordeprecated
- 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
- func (c *Ini) SectionKeys(withDefSection bool) (ls []string)
- func (c *Ini) Set(key string, val any, section ...string) (err error)
- func (c *Ini) SetSection(name string, values map[string]string) (err error)
- func (c *Ini) String(key string, defVal ...string) string
- func (c *Ini) StringMap(name string) (mp map[string]string)
- func (c *Ini) Strings(key string, sep ...string) (ss []string)
- func (c *Ini) Uint(key string, defVal ...uint) (value uint)
- 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 = "." DefTagName = "ini" )
some default constants
Variables ¶
This section is empty.
Functions ¶
func GetValue ¶
GetValue get a value by key string. you can use '.' split for get value in a special section
func LoadExists ¶
LoadExists load files, will ignore not exists
func ParseEnv ¶
func ParseEnv(opts *Options)
ParseEnv will parse ENV key on get value
Usage:
ini.NewWithOptions(ini.ParseEnv)
func ParseVar ¶
func ParseVar(opts *Options)
ParseVar on get value
Usage:
ini.NewWithOptions(ini.ParseVar)
func Readonly ¶
func Readonly(opts *Options)
Readonly setting
Usage:
ini.NewWithOptions(ini.Readonly)
func SectionKeys ¶ added in v2.0.3
SectionKeys get all section names
Types ¶
type Ini ¶
type Ini struct {
// contains filtered or unexported fields
}
Ini config data manager
func NewWithOptions ¶
NewWithOptions new an instance and with some options
Usage:
ini.NewWithOptions(ini.ParseEnv, ini.Readonly)
func (*Ini) Bool ¶
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.
The `value` boolean will be false in the event that the value could not be parsed as a bool
func (*Ini) DelSection ¶
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) GetValue ¶
GetValue 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) MapStruct ¶ added in v2.0.6
MapStruct get config data and binding to the structure. If the key is empty, will bind all data to the struct ptr.
Usage:
user := &Db{} ini.MapStruct("user", &user)
func (*Ini) NewSection ¶
NewSection add new section data, existed will be replaced
func (*Ini) PrettyJSON ¶
PrettyJSON translate to pretty JSON string
func (*Ini) SectionKeys ¶ added in v2.0.3
SectionKeys get all section names
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 existed, will merge to old section.
func (*Ini) WithOptions ¶
WithOptions apply some options
type Options ¶
type Options struct { // Readonly set to read-only mode. default False Readonly bool // TagName for binding struct TagName string // ParseEnv parse ENV var name. default True ParseEnv bool // ParseVar parse variable reference "%(varName)s". default False ParseVar bool // ReplaceNl replace the "\n" to newline ReplaceNl bool // VarOpen var left open char. default "%(" VarOpen string // VarClose var right close char. default ")s" VarClose string // IgnoreCase ignore key name case. default False IgnoreCase bool // DefSection default section name. default "__default", it's allow empty string. DefSection string // SectionSep sep char for split key path. default ".", use like "section.subKey" SectionSep string }
Options for config
func GetOptions ¶ added in v2.0.3
func GetOptions() Options
GetOptions get options info.
Notice: return is value. so, cannot change Ini instance