欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

使用示例

創(chuàng)建一個 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")
# 使用環(huán)境變量開啟測試模式,無需修改代碼
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")
# 使用環(huán)境變量開啟詳細模式,無需修改代碼
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 的優(yōu)先級 大于 database.json, 排在前面的配置文件優(yōu)先級大于排在后的的配置
configor.Load(&Config, "application.yml", "database.json")

根據(jù)環(huán)境變量加載配置文件

使用CONFIGOR_ENV 設置環(huán)境變量,如果 CONFIGOR_ENV 沒有設置,框架將會使用 development 作為默認環(huán)境變量。

// 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` 的配置
// 在代碼里面設置 環(huán)境變量
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 參數(shù)的環(huán)境變量將會變?yōu)?CONFIGOR_DESCRIPTION,否則環(huán)境變量將會是 CONFIGOR_DETAILS_DESCRIPTION

到此這篇關于Golang Configor配置文件工具的使用詳解的文章就介紹到這了,更多相關go configor內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Go類型斷言提取測試接口值動態(tài)類型及靜態(tài)轉(zhuǎn)換確保類型接口編譯安全

    Go類型斷言提取測試接口值動態(tài)類型及靜態(tài)轉(zhuǎn)換確保類型接口編譯安全

    這篇文章主要為大家介紹了Go類型斷言提取測試接口值動態(tài)類型及靜態(tài)轉(zhuǎn)換確保類型實現(xiàn)特定接口的編譯時安全性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    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如何優(yōu)雅的使用字節(jié)池示例詳解

    Go如何優(yōu)雅的使用字節(jié)池示例詳解

    在編程開發(fā)中,我們經(jīng)常會需要頻繁創(chuàng)建和銷毀同類對象的情形,這樣的操作很可能會對性能造成影響,這時常用的優(yōu)化手段就是使用對象池(object pool),這篇文章主要給大家介紹了關于Go如何優(yōu)雅的使用字節(jié)池的相關資料,需要的朋友可以參考下
    2022-08-08
  • 一文帶你深入了解Go語言中切片的奧秘

    一文帶你深入了解Go語言中切片的奧秘

    切片是數(shù)組的一個引用,因此切片是引用類型。但自身是結構體,值拷貝傳遞。本文將通過示例帶大家一起探索一下Go語言中切片的奧秘,感興趣的可以了解一下
    2022-11-11
  • Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式

    Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式

    這篇文章主要介紹了Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Golang中的變量學習小結

    Golang中的變量學習小結

    本文主要帶大家學習了Golang里面的四大類型的變量,十分的詳細,有需要的小伙伴可以參考下
    2018-10-10
  • 使用go語言實現(xiàn)Redis持久化的示例代碼

    使用go語言實現(xiàn)Redis持久化的示例代碼

    redis 是一個內(nèi)存數(shù)據(jù)庫,如果你把進程殺掉,那么里面存儲的數(shù)據(jù)都會消失,那么這篇文章就是來解決 redis 持久化的問題,本文給大家介紹了使用go語言實現(xiàn)Redis持久化,需要的朋友可以參考下
    2024-07-07
  • 一文帶你了解Go語言中的類型斷言和類型轉(zhuǎn)換

    一文帶你了解Go語言中的類型斷言和類型轉(zhuǎn)換

    在Go中,類型斷言和類型轉(zhuǎn)換是一個令人困惑的事情,他們似乎都在做同樣的事情。最明顯的不同點是他們具有不同的語法(variable.(type)?vs?type(variable)?)。本文我們就來深入研究一下二者的區(qū)別
    2022-09-09
  • Go1.18?新特性之多模塊Multi-Module工作區(qū)模式

    Go1.18?新特性之多模塊Multi-Module工作區(qū)模式

    這篇文章主要介紹了Go1.18?新特性之多模塊Multi-Module工作區(qū)模式,在 Go 1.18之前,建議使用依賴模塊中的 replace 指令來處理這個問題,從 Go 1.18開始引入了一種同時處理多個模塊的新方法,通過案例給大家詳細介紹,感興趣的朋友一起看看吧
    2022-04-04

最新評論