Golang?Configor配置文件工具的使用詳解
介紹
一個(gè)支持 yaml、json、toml、shell 的配置文件工具
安裝
go get github.com/jinzhu/configor
or
gopm get -v github.com/jinzhu/configor
使用示例
創(chuàng)建一個(gè) 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)
}測(cè)試模式
Usage:
// 上面的代碼 下面這這句
err := configor.Load(&conf, "config.yml")
// 修改為
err := configor.New(&configor.Config{Debug: true}).Load(&conf, "config.yml")# 使用環(huán)境變量開(kāi)啟測(cè)試模式,無(wú)需修改代碼 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}]}
詳細(xì)模式
Usage:
// 上面的代碼 下面這這句
err := configor.Load(&conf, "config.yml")
// 修改為
err := configor.New(&configor.Config{Verbose: true}).Load(&conf, "config.yml")# 使用環(huán)境變量開(kāi)啟詳細(xì)模式,無(wú)需修改代碼 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}]}
高級(jí)用法
加載多個(gè)配置文件
// application.yml 的優(yōu)先級(jí) 大于 database.json, 排在前面的配置文件優(yōu)先級(jí)大于排在后的的配置 configor.Load(&Config, "application.yml", "database.json")
根據(jù)環(huán)境變量加載配置文件
使用CONFIGOR_ENV 設(shè)置環(huán)境變量,如果 CONFIGOR_ENV 沒(méi)有設(shè)置,框架將會(huì)使用 development 作為默認(rèn)環(huán)境變量。
// config.go configor.Load(&Config, "config.json") $ go run config.go // 將會(huì)加載 `config.json`,如果存在 `config.development.json` 將會(huì)自動(dòng)加載 // `config.development.json` 將會(huì)覆蓋 `config.json` 的配置 $ CONFIGOR_ENV=production go run config.go // 將會(huì)加載 `config.json`,如果存在 `config.production.json` 將會(huì)自動(dòng)加載 // `config.production.json` 將會(huì)覆蓋 `config.json` 的配置 $ go test // 將會(huì)加載 `config.json`,如果存在 `config.test.json` 將會(huì)自動(dòng)加載 // `config.test.json` 將會(huì)覆蓋 `config.json` 的配置 $ CONFIGOR_ENV=production go test // 將會(huì)加載 `config.json`,如果存在 `config.production.json` 將會(huì)自動(dòng)加載 // `config.production.json` 將會(huì)覆蓋 `config.json` 的配置
// 在代碼里面設(shè)置 環(huán)境變量
configor.New(&configor.Config{Environment: "production"}).Load(&Config, "config.json")示例配置
configor.Load(&Config, "config.yml") $ go run config.go # 將會(huì)自動(dòng)加載 `config.example.yml` 如果 `config.yml` 不存在將會(huì)輸出警告信息
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加載配置項(xiàng)
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")匿名結(jié)構(gòu) - Anonymous Struct
type Details Struct {
Description string
}
type Config struct {
Details `anonymous:"true"`
}如果使用 anonymous:"true" 標(biāo)簽,那么 Description 參數(shù)的環(huán)境變量將會(huì)變?yōu)?CONFIGOR_DESCRIPTION,否則環(huán)境變量將會(huì)是 CONFIGOR_DETAILS_DESCRIPTION
到此這篇關(guān)于Golang Configor配置文件工具的使用詳解的文章就介紹到這了,更多相關(guān)go configor內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文帶你吃透Golang中net/http標(biāo)準(zhǔn)庫(kù)服務(wù)端
這篇文章將從服務(wù)端(Server)作為切入點(diǎn)和大家分享一下Go語(yǔ)言net/http標(biāo)準(zhǔn)庫(kù)的實(shí)現(xiàn)邏輯,進(jìn)而一步步分析http標(biāo)準(zhǔn)庫(kù)內(nèi)部是如何運(yùn)作的,感興趣的可以了解下2024-03-03
淺析go中Ticker,Timer和Tick的用法與區(qū)別
在go面試的時(shí)候,面試官經(jīng)常會(huì)問(wèn)time包的Ticker,Timer以及Tick的區(qū)別,一般在超時(shí)控制的時(shí)候用的比較多,今天就跟隨小編一起來(lái)詳細(xì)學(xué)一下這幾個(gè)的區(qū)別吧2023-10-10
Golang實(shí)現(xiàn)多存儲(chǔ)驅(qū)動(dòng)設(shè)計(jì)SDK案例
這篇文章主要介紹了Golang實(shí)現(xiàn)多存儲(chǔ)驅(qū)動(dòng)設(shè)計(jì)SDK案例,Gocache是一個(gè)基于Go語(yǔ)言編寫的多存儲(chǔ)驅(qū)動(dòng)的緩存擴(kuò)展組件,更多具體內(nèi)容感興趣的小伙伴可以參考一下2022-09-09
如何使用?Go?和?Excelize?構(gòu)建電子表格
這篇文章主要介紹了如何使用Go和Excelize構(gòu)建電子表格,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
golang泛型Generics的實(shí)現(xiàn)
本文主要介紹了golang泛型Generics的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2026-04-04
Golang自定義結(jié)構(gòu)體轉(zhuǎn)map的操作
這篇文章主要介紹了Golang自定義結(jié)構(gòu)體轉(zhuǎn)map的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
golang進(jìn)程內(nèi)存控制避免docker內(nèi)oom
這篇文章主要為大家介紹了golang進(jìn)程內(nèi)存控制避免docker內(nèi)oom示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
GOPROXY:解決go get golang.org/x包失敗問(wèn)題
這篇文章主要介紹了GOPROXY:解決go get golang.org/x包失敗問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

