viper

viper 是一个残缺的Go应用程序配置解决方案。它被设计为在应用程序中工作,能够解决所有类型的配置需要和格局。它反对:
  • 反对JSON, TOML, YAML, HCL, envfile,以及java等丰盛的配置文件
  • 实时查看和从新读取配置文件(可选)
  • 从近程配置零碎(etcd或Consul)读取数据,并察看更改
  • 从命令行标记读取

实现目录如下

.├── config│   ├── app.go //│   └── config.go //配置文件├── go.mod├── go.sum├── main.go└── pkg    └── config        └── config.go 

应用

在你的我的项目目录下执行go mod init
 go get github.com/spf13/viper

新建pkg/config 并创立config.go文件

/**  @author:panliang  @data:2021/5/13  @note**/package configimport (    "github.com/spf13/cast"    "github.com/spf13/viper"    "log")var Viper *viper.Vipertype StrMap map[string]interface{}func init()  {    Viper = viper.New()    // 1. 初始化 Viper 库    Viper = viper.New()    // 2. 设置文件名称    Viper.SetConfigName(".env")    // 3. 配置类型,反对 "json", "toml", "yaml", "yml", "properties",    //             "props", "prop", "env", "dotenv"    Viper.SetConfigType("env")    // 4. 环境变量配置文件查找的门路,绝对于 main.go    Viper.AddConfigPath(".")    // 5. 开始读根目录下的 .env 文件,读不到会报错    err := Viper.ReadInConfig()    log.Println(err)    // 6. 设置环境变量前缀,用以辨别 Go 的零碎环境变量    Viper.SetEnvPrefix("appenv")    // 7. Viper.Get() 时,优先读取环境变量    Viper.AutomaticEnv()}// Env 读取环境变量,反对默认值func Env(envName string, defaultValue ...interface{}) interface{} {    if len(defaultValue) > 0 {        return Get(envName, defaultValue[0])    }    return Get(envName)}// Add 新增配置项func Add(name string, configuration map[string]interface{}) {    Viper.Set(name, configuration)}// Get 获取配置项,容许应用点式获取,如:app.namefunc Get(path string, defaultValue ...interface{}) interface{} {    // 不存在的状况    if !Viper.IsSet(path) {        if len(defaultValue) > 0 {            return defaultValue[0]        }        return nil    }    return Viper.Get(path)}// GetString 获取 String 类型的配置信息func GetString(path string, defaultValue ...interface{}) string {    return cast.ToString(Get(path, defaultValue...))}// GetInt 获取 Int 类型的配置信息func GetInt(path string, defaultValue ...interface{}) int {    return cast.ToInt(Get(path, defaultValue...))}// GetInt64 获取 Int64 类型的配置信息func GetInt64(path string, defaultValue ...interface{}) int64 {    return cast.ToInt64(Get(path, defaultValue...))}// GetUint 获取 Uint 类型的配置信息func GetUint(path string, defaultValue ...interface{}) uint {    return cast.ToUint(Get(path, defaultValue...))}// GetBool 获取 Bool 类型的配置信息func GetBool(path string, defaultValue ...interface{}) bool {    return cast.ToBool(Get(path, defaultValue...))}

创立config目录以及app.go

/**  @author:panliang  @data:2021/5/13  @note**/package configimport "gotime/pkg/config"func init() {    config.Add("app", config.StrMap{        // 利用名称,临时没有应用到        "name": config.Env("APP_NAME", "test"),        // 以后环境,用以辨别多环境        "env": config.Env("APP_ENV", "production"),    })}

主动读取config目录下配置创立config.go

/**  @author:panliang  @data:2021/5/13  @note**/package configfunc Initialize() {    // 触发加载本目录下其余文件中的 init 办法}

新建main.go

/**  @author:panliang  @data:2021/5/13  @note**/package mainimport (    "fmt"    "gotime/config"    config2 "gotime/pkg/config")func init()  {    //初始化加载    config.Initialize()}func main()  {      //打印    fmt.Println(config2.Get("app.name"))}

新建配置.env

APP_NAME=testsAPP_ENV=local

执行

go run main.go