提供一个子包 dotenv,支持从文件(eg .env)中导入数据到ENV
如果你仅仅想用INI来做简单配置管理,推荐使用 gookit/ini
这里使用yaml格式作为示例(testdata/yml_other.yml):
name: app2
debug: false
baseKey: value2
shell: ${SHELL}
envKey1: ${NotExist|defValue}
map1:
key: val2
key2: val20
arr1:
- val1
- val21
示例代码请看 _examples/yaml.go:
package main
import (
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yaml"
)
// go run ./examples/yaml.go
func main() {
// 设置选项支持 ENV 解析
config.WithOptions(config.ParseEnv)
// 添加驱动程序以支持yaml内容解析(除了JSON是默认支持,其他的则是按需使用)
config.AddDriver(yaml.Driver)
// 加载配置,可以同时传入多个文件
err := config.LoadFiles("testdata/yml_base.yml")
if err != nil {
panic(err)
}
// fmt.Printf("config data: \n %#v\n", config.Data())
// 加载更多文件
err = config.LoadFiles("testdata/yml_other.yml")
// can also load multi at once
// err := config.LoadFiles("testdata/yml_base.yml", "testdata/yml_other.yml")
if err != nil {
panic(err)
}
}
注意:结构体默认的绑定映射tag是 mapstructure,可以通过设置 Options.TagName 来更改它
user := struct {
Age int
Kye string
UserName string `mapstructure:"user_name"`
Tags []int
}{}
err = config.BindStruct("user", &user)
fmt.Println(user.UserName) // inhere
更改结构标签名称
config.WithOptions(func(opt *Options) {
opt.TagName = "config"
})
可以使用空字符串将所有配置数据绑定到结构:
config.BindStruct("", &myConf)
// 获取整型
age := config.Int("age")
fmt.Print(age) // 100
// 获取布尔值
val := config.Bool("debug")
fmt.Print(val) // true
// 获取字符串
name := config.String("name")
fmt.Print(name) // inhere
// 获取字符串数组
arr1 := config.Strings("arr1")
fmt.Printf("%v %#v", arr1) // []string{"val1", "val21"}
// 获取字符串KV映射
val := config.StringMap("map1")
fmt.Printf("%v %#v",val) // map[string]string{"key":"val2", "key2":"val20"}
// 值包含ENV变量
value := config.String("shell")
fmt.Print(value) // /bin/zsh
// 通过key路径获取值
// from array
value := config.String("arr1.0")
fmt.Print(value) // "val1"
// from map
value := config.String("map1.key")
fmt.Print(value) // "val2"
// set value
config.Set("name", "new name")
// get
name = config.String("name")
fmt.Print(name) // new name
// os env: APP_NAME=config APP_DEBUG=true
// load ENV info
config.LoadOSEnv([]string{"APP_NAME", "APP_NAME"}, true)
// read
config.Bool("app_debug") // true
config.String("app_name") // "config"
支持简单的命令行 flag 参数解析,加载数据
// flags like: --name inhere --env dev --age 99 --debug
// load flag info
keys := []string{"name", "env", "age:int" "debug:bool"}
err := config.LoadFlags(keys)
// read
config.String("name") // "inhere"
config.String("env") // "dev"
config.Int("age") // 99
config.Bool("debug") // true
您可以创建自定义配置实例:
// create new instance, will auto register JSON driver
myConf := config.New("my-conf")
// create empty instance
myConf := config.NewEmpty("my-conf")
// create and with some options
myConf := config.NewWithOptions("my-conf", config.ParseEnv, config.ReadOnly)
// Options config options
type Options struct {
// parse env value. like: "${EnvName}" "${EnvName|default}"
ParseEnv bool
// config is readonly. default is False
Readonly bool
// enable config data cache. default is False
EnableCache bool
// parse key, allow find value by key path. default is True eg: 'key.sub' will find `map[key]sub`
ParseKey bool
// tag name for binding data to struct
TagName string
// the delimiter char for split key, when `FindByPath=true`. default is '.'
Delimiter byte
// default write format. default is JSON
DumpFormat string
// default input format. default is JSON
ReadFormat string
}
github 地址:https://github.com/gookit/config

