Documentation
¶
Overview ¶
golang application config manage implement.
Source code and other details for the project are available at GitHub:
https://github.com/gookit/config
Here using the yaml format as an example(yml_other.yml):
name: app2
debug: false
baseKey: value2
map1:
key: val2
key2: val20
arr1:
- val1
- val21
usage:
import (
"github.com/gookit/config"
"github.com/gookit/config/yaml"
"fmt"
)
// add yaml decoder
config.SetDecoder(config.Yaml, yaml.Decoder)
config.LoadFiles("testdata/yml_other.yml")
name, ok := config.GetString("name")
fmt.Printf("get 'name', ok: %v, val: %#v\n", ok, name)
arr1, ok := config.GetStringArr("arr1")
fmt.Printf("get 'arr1', ok: %v, val: %#v\n", ok, arr1)
val0, ok := config.GetString("arr1.0")
fmt.Printf("get sub 'arr1.0', ok: %v, val: %#v\n", ok, val0)
map1, ok := config.GetStringMap("map1")
fmt.Printf("get 'map1', ok: %v, val: %#v\n", ok, map1)
val0, ok = config.GetString("map1.key")
fmt.Printf("get sub 'map1.key', ok: %v, val: %#v\n", ok, val0)
output:
get 'name', ok: true, val: "app2"
get 'arr1', ok: true, val: []string{"val1", "val21"}
get sub 'arr1.0', ok: true, val: "val1"
get 'map1', ok: true, val: map[string]string{"key":"val2", "key2":"val20"}
get sub 'map1.key', ok: true, val: "val2"
Index ¶
- Constants
- func Data() map[string]interface{}
- func DefBool(key string, def bool) bool
- func DefInt(key string, def int) int
- func DefString(key string, def string) string
- func Get(key string, findByPath ...bool) (value interface{}, ok bool)
- func GetBool(key string) (value bool, ok bool)
- func GetInt(key string) (value int, ok bool)
- func GetString(key string) (value string, ok bool)
- func GetStringArr(key string) (arr []string, ok bool)
- func GetStringMap(key string) (mp map[string]string, ok bool)
- func LoadData(dataSource ...interface{}) (err error)
- func LoadFiles(sourceFiles ...string) (err error)
- func LoadSources(format string, sourceCode ...[]byte) (err error)
- func Set()
- func SetDecoder(format string, decoder Decoder)
- func SetOptions(opts *Options)
- type Config
- func (c *Config) ClearAll()
- func (c *Config) ClearCaches()
- func (c *Config) ClearData()
- func (c *Config) Data() map[string]interface{}
- func (c *Config) DefBool(key string, def bool) bool
- func (c *Config) DefInt(key string, def int) int
- func (c *Config) DefString(key string, def string) string
- func (c *Config) Get(key string, findByPath ...bool) (value interface{}, ok bool)
- func (c *Config) GetBool(key string) (value bool, ok bool)
- func (c *Config) GetInt(key string) (value int, ok bool)
- func (c *Config) GetString(key string) (value string, ok bool)
- func (c *Config) GetStringArr(key string) (arr []string, ok bool)
- func (c *Config) GetStringMap(key string) (mp map[string]string, ok bool)
- func (c *Config) GetStructure(key string, v interface{}) (err error)
- func (c *Config) HasDecoder(format string) bool
- func (c *Config) LoadData(dataSources ...interface{}) (err error)
- func (c *Config) LoadExists(sourceFiles ...string) (err error)
- func (c *Config) LoadFiles(sourceFiles ...string) (err error)
- func (c *Config) LoadSources(format string, sourceCodes ...[]byte) (err error)
- func (c *Config) MapStructure(key string, v interface{}) (err error)
- func (c *Config) Name() string
- func (c *Config) Readonly(readonly bool)
- func (c *Config) SetDecoder(format string, decoder Decoder)
- func (c *Config) SetOptions(opts *Options)
- type Decoder
- type Encoder
- type Options
Constants ¶
const ( Json = "json" Yml = "yml" Yaml = "yaml" Toml = "toml" )
supported config format
const ( Version = "0.0.1" DefaultNode = "__DEFAULT" )
Variables ¶
This section is empty.
Functions ¶
func GetStringMap ¶
GetStringMap get config data as a map[string]string
func SetDecoder ¶
SetDecoder add/set a format decoder
func SetOptions ¶
func SetOptions(opts *Options)
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config
func (*Config) Get ¶
Get config value by key string, support get sub-value by key path(eg. 'map.key'), ok is true, find value from config ok is false, not found or error
func (*Config) GetBool ¶
GetBool 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
- yes
- false
- no
- 1
- 0
The `ok` boolean will be false in the event that the value could not be parsed as a bool
func (*Config) GetStringArr ¶
GetStringArr get config data as a slice/array
func (*Config) GetStringMap ¶
GetStringMap get config data as a map[string]string
func (*Config) GetStructure ¶
GetStructure get config data and map to a structure. usage:
dbInfo := Db{}
config.GetStructure("db", &dbInfo)
func (*Config) LoadExists ¶
LoadExists load and parse config files, but will ignore not exists file.
func (*Config) LoadSources ¶
LoadSources load data from byte content. usage:
config.LoadSources(config.Yml, []byte(` name: blog arr: key: val
`))
func (*Config) MapStructure ¶
MapStructure alias method of the 'GetStructure'