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 微服务开发框架DMicro设计思路详解

    Go 微服务开发框架DMicro设计思路详解

    这篇文章主要为大家介绍了Go 微服务开发框架DMicro设计思路详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Golang errgroup 设计及实现原理解析

    Golang errgroup 设计及实现原理解析

    这篇文章主要为大家介绍了Golang errgroup 设计及实现原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • 提升Go语言开发效率的小技巧实例(GO语言语法糖)汇总

    提升Go语言开发效率的小技巧实例(GO语言语法糖)汇总

    这篇文章主要介绍了提升Go语言开发效率的小技巧汇总,也就是Go语言的语法糖,掌握好这些可以提高我们的开发效率,需要的朋友可以参考下
    2022-11-11
  • golang进行简单权限认证的实现

    golang进行简单权限认证的实现

    本文主要介绍了golang简单权限认证的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 一文带大家了解Go语言中的内联优化

    一文带大家了解Go语言中的内联优化

    内联优化是一种常见的编译器优化策略,通俗来讲,就是把函数在它被调用的地方展开,这样可以减少函数调用所带来的开销,本文主要为大家介绍了Go中内联优化的具体使用,需要的可以参考下
    2023-05-05
  • golang关闭chan通道的方法示例

    golang关闭chan通道的方法示例

    在go语言中,通道(channel)是一个非常重要的概念,通道提供了一种在不同 goroutine 之间安全地传递数据的方式,在本文中,我们将讨论如何关闭通道以及在关闭通道时需要考虑的事项,需要的朋友可以参考下
    2024-02-02
  • Golang中的map操作方法详解

    Golang中的map操作方法详解

    这篇文章主要给大家介绍了关于Golang中map操作方法的相关资料,map是一种无序的基于key-value的数据结构,Go语言中map是引用类型,必须初始化才能使用,需要的朋友可以参考下
    2023-11-11
  • Go语言rune与字符串转换的密切关系解析

    Go语言rune与字符串转换的密切关系解析

    这篇文章主要为大家介绍了Go语言rune与字符串转换的密切关系示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • golang如何设置Header Content-type

    golang如何设置Header Content-type

    这篇文章主要介绍了golang如何设置Header Content-type问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • 一文详解Go Http Server原理

    一文详解Go Http Server原理

    这篇文章主要为大家介绍了Go Http Server原理示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01

最新评论