Golang Configor配置文件工具的使用详解

 更新时间:2023年08月29日 10:53:42   作者:Arrows  
Configor是一个支持 yaml、json、toml、shell 的配置文件工具,这篇文中主要为大家详细介绍了Configor的具体使用,感兴趣的小伙伴可以学习一下

介绍

一个支持 yaml、json、toml、shell 的配置文件工具

安装

go get github.com/jinzhu/configor

or

gopm get -v github.com/jinzhu/configor

使用示例

创建一个 yaml 配置文件,config.yml

appname: test
db:
  name:     test
  user:     root
  password: 123
  port:     3306
contacts:
  - name:  jack
    email: jack@test.com
  - name:  tom
    email: tom@test.com

编写代码:

package main
import (
    "fmt"
    "github.com/jinzhu/configor"
)
type Config struct {
    APPName string `default:"app name"`
    DB struct{
        Name     string
        User     string `default:"root"`
        Password string `required:"true" env:"DBPassword"`
        Port     uint   `default:"3306"`
    }
    Contacts []struct{
        Name  string
        Email string `required:"true"`
    }
}
func main()  {
    var conf = Config{}
    err := configor.Load(&conf, "config.yml")
    if err != nil {
        panic(err)
    }
    fmt.Printf("%v \n", conf)
}

测试模式

Usage:

// 上面的代码 下面这这句
err := configor.Load(&conf, "config.yml")
// 修改为
err := configor.New(&configor.Config{Debug: true}).Load(&conf, "config.yml")
# 使用环境变量开启测试模式,无需修改代码
CONFIGOR_DEBUG_MODE=true go run config.go 

Output:

Current environment: 'development'
Loading configurations from file 'config.yml'...
Configuration:
  &main.Config{APPName:"test", DB:struct { Name string; User string "default:\"root\""; Password string "required:\"true\" env:\"DBPassword\""; Port uint "default:\"3306\"" }{Name:"test", User:"root", Password:"123", Port:0xcea}, Contacts:[]struct { Name string; Email string "required:\"true\"" }{struct { Name string; Email string "required:\"true\"" }{Name:"jack", Email:"jack@test.com"}, struct { Name string; Email string "required:\"true\"" }{Name:"tom", Email:"tom@test.com"}}}
{test {test root 123 3306} [{jack jack@test.com} {tom tom@test.com}]} 

详细模式

Usage:

// 上面的代码 下面这这句
err := configor.Load(&conf, "config.yml")
// 修改为
err := configor.New(&configor.Config{Verbose: true}).Load(&conf, "config.yml")
# 使用环境变量开启详细模式,无需修改代码
CONFIGOR_VERBOSE_MODE=true go run config.go 

Output:

Current environment: 'development'
Loading configurations from file 'config.yml'...
Trying to load struct `Config`'s field `APPName` from env Configor_APPName, CONFIGOR_APPNAME
Trying to load struct `Config`'s field `DB` from env Configor_DB, CONFIGOR_DB
Trying to load struct ``'s field `Name` from env Configor_DB_Name, CONFIGOR_DB_NAME
Trying to load struct ``'s field `User` from env Configor_DB_User, CONFIGOR_DB_USER
Trying to load struct ``'s field `Password` from env DBPassword
Trying to load struct ``'s field `Port` from env Configor_DB_Port, CONFIGOR_DB_PORT
Trying to load struct `Config`'s field `Contacts` from env Configor_Contacts, CONFIGOR_CONTACTS
Trying to load struct ``'s field `Name` from env Configor_Contacts_0_Name, CONFIGOR_CONTACTS_0_NAME
Trying to load struct ``'s field `Email` from env Configor_Contacts_0_Email, CONFIGOR_CONTACTS_0_EMAIL
Trying to load struct ``'s field `Name` from env Configor_Contacts_1_Name, CONFIGOR_CONTACTS_1_NAME
Trying to load struct ``'s field `Email` from env Configor_Contacts_1_Email, CONFIGOR_CONTACTS_1_EMAIL
Configuration:
  &main.Config{APPName:"test", DB:struct { Name string; User string "default:\"root\""; Password string "required:\"true\" env:\"DBPassword\""; Port uint "default:\"3306\"" }{Name:"test", User:"root", Password:"123", Port:0xcea}, Contacts:[]struct { Name string; Email string "required:\"true\"" }{struct { Name string; Email string "required:\"true\"" }{Name:"jack", Email:"jack@test.com"}, struct { Name string; Email string "required:\"true\"" }{Name:"tom", Email:"tom@test.com"}}}
{test {test root 123 3306} [{jack jack@test.com} {tom tom@test.com}]}

高级用法

加载多个配置文件

// application.yml 的优先级 大于 database.json, 排在前面的配置文件优先级大于排在后的的配置
configor.Load(&Config, "application.yml", "database.json")

根据环境变量加载配置文件

使用CONFIGOR_ENV 设置环境变量,如果 CONFIGOR_ENV 没有设置,框架将会使用 development 作为默认环境变量。

// config.go
configor.Load(&Config, "config.json")
$ go run config.go
// 将会加载 `config.json`,如果存在 `config.development.json` 将会自动加载
// `config.development.json` 将会覆盖 `config.json` 的配置
$ CONFIGOR_ENV=production go run config.go
// 将会加载 `config.json`,如果存在 `config.production.json` 将会自动加载
// `config.production.json` 将会覆盖 `config.json` 的配置
$ go test
// 将会加载 `config.json`,如果存在 `config.test.json` 将会自动加载
// `config.test.json` 将会覆盖 `config.json` 的配置
$ CONFIGOR_ENV=production go test
// 将会加载 `config.json`,如果存在 `config.production.json` 将会自动加载
// `config.production.json` 将会覆盖 `config.json` 的配置
// 在代码里面设置 环境变量
configor.New(&configor.Config{Environment: "production"}).Load(&Config, "config.json")

示例配置

configor.Load(&Config, "config.yml")
$ go run config.go
# 将会自动加载 `config.example.yml` 如果 `config.yml` 不存在将会输出警告信息

Output:

Failed to find configuration config.yml, using example file config.example.yml
{test {dodododo root 123 3306 } [{jack jack@test.com} {tom tom@test.com}]} 

从shell加载配置项

CONFIGOR_APPNAME="hello world" go run config.go
# 格式为 {{prefix}}_FieldName
# 覆盖 prefix shell 操作
$ CONFIGOR_ENV_PREFIX="WEB" WEB_APPNAME="hello, prefix" go run config.go
# 在代码的书写方式为
configor.New(&configor.Config{ENVPrefix: "WEB"}).Load(&Config, "config.json")

匿名结构 - Anonymous Struct

type Details Struct {
    Description string
}
type Config struct {
    Details `anonymous:"true"`
}

如果使用 anonymous:"true" 标签,那么 Description 参数的环境变量将会变为 CONFIGOR_DESCRIPTION,否则环境变量将会是 CONFIGOR_DETAILS_DESCRIPTION

到此这篇关于Golang Configor配置文件工具的使用详解的文章就介绍到这了,更多相关go configor内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Go类型断言提取测试接口值动态类型及静态转换确保类型接口编译安全

    Go类型断言提取测试接口值动态类型及静态转换确保类型接口编译安全

    这篇文章主要为大家介绍了Go类型断言提取测试接口值动态类型及静态转换确保类型实现特定接口的编译时安全性详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • GO语言(golang)基础知识

    GO语言(golang)基础知识

    这篇文章主要介绍了GO语言(golang)基础知识,需要的朋友可以参考下
    2015-01-01
  • Golang 1.16 中 Modules的主要变化更新

    Golang 1.16 中 Modules的主要变化更新

    这篇文章主要介绍了Golang 1.16 中 Modules的主要变化更新,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • Go如何优雅的使用字节池示例详解

    Go如何优雅的使用字节池示例详解

    在编程开发中,我们经常会需要频繁创建和销毁同类对象的情形,这样的操作很可能会对性能造成影响,这时常用的优化手段就是使用对象池(object pool),这篇文章主要给大家介绍了关于Go如何优雅的使用字节池的相关资料,需要的朋友可以参考下
    2022-08-08
  • 一文带你深入了解Go语言中切片的奥秘

    一文带你深入了解Go语言中切片的奥秘

    切片是数组的一个引用,因此切片是引用类型。但自身是结构体,值拷贝传递。本文将通过示例带大家一起探索一下Go语言中切片的奥秘,感兴趣的可以了解一下
    2022-11-11
  • Golang IPv4 字符串与整数互转方式

    Golang IPv4 字符串与整数互转方式

    这篇文章主要介绍了Golang IPv4 字符串与整数互转方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • Golang中的变量学习小结

    Golang中的变量学习小结

    本文主要带大家学习了Golang里面的四大类型的变量,十分的详细,有需要的小伙伴可以参考下
    2018-10-10
  • 使用go语言实现Redis持久化的示例代码

    使用go语言实现Redis持久化的示例代码

    redis 是一个内存数据库,如果你把进程杀掉,那么里面存储的数据都会消失,那么这篇文章就是来解决 redis 持久化的问题,本文给大家介绍了使用go语言实现Redis持久化,需要的朋友可以参考下
    2024-07-07
  • 一文带你了解Go语言中的类型断言和类型转换

    一文带你了解Go语言中的类型断言和类型转换

    在Go中,类型断言和类型转换是一个令人困惑的事情,他们似乎都在做同样的事情。最明显的不同点是他们具有不同的语法(variable.(type) vs type(variable) )。本文我们就来深入研究一下二者的区别
    2022-09-09
  • Go1.18 新特性之多模块Multi-Module工作区模式

    Go1.18 新特性之多模块Multi-Module工作区模式

    这篇文章主要介绍了Go1.18 新特性之多模块Multi-Module工作区模式,在 Go 1.18之前,建议使用依赖模块中的 replace 指令来处理这个问题,从 Go 1.18开始引入了一种同时处理多个模块的新方法,通过案例给大家详细介绍,感兴趣的朋友一起看看吧
    2022-04-04

最新评论