introduce
A configuration file tool that supports yaml, json, toml, shell
Install
go get /jinzhu/configor
or
gopm get -v /jinzhu/configor
Example of usage
Create a yaml configuration file,
appname: test db: name: test user: root password: 123 port: 3306 contacts: - name: jack email: jack@ - name: tom email: tom@
Write code:
package main import ( "fmt" "/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 := (&conf, "") if err != nil { panic(err) } ("%v \n", conf) }
Test mode
Usage:
// The above code The following sentenceerr := (&conf, "") // Modify toerr := (&{Debug: true}).Load(&conf, "")
# Use environment variables to enable test mode without modifying the codeCONFIGOR_DEBUG_MODE=true go run
Output:
Current environment: 'development'
Loading configurations from file ''...
Configuration:
&{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@"}, struct { Name string; Email string "required:\"true\"" }{Name:"tom", Email:"tom@"}}}
{test {test root 123 3306} [{jack jack@} {tom tom@}]}
Detailed mode
Usage:
// The above code The following sentenceerr := (&conf, "") // Modify toerr := (&{Verbose: true}).Load(&conf, "")
# Use environment variables to enable detailed mode without modifying the codeCONFIGOR_VERBOSE_MODE=true go run
Output:
Current environment: 'development'
Loading configurations from file ''...
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:
&{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@"}, struct { Name string; Email string "required:\"true\"" }{Name:"tom", Email:"tom@"}}}
{test {test root 123 3306} [{jack jack@} {tom tom@}]}
Advanced Usage
Load multiple configuration files
// The priority of the configuration file is greater than the priority of the configuration file is greater than the configuration file is lower than the configuration file is lower(&Config, "", "")
Load configuration files according to environment variables
useCONFIGOR_ENV
Set environment variables, ifCONFIGOR_ENV
Without settings, the framework will usedevelopment
As the default environment variable.
// (&Config, "") $ go run // Will load ``, if there is ``, it will load automatically// `` will overwrite the `` configuration$ CONFIGOR_ENV=production go run // Will load ``, if there is ``, it will load automatically// `` will overwrite the `` configuration$ go test // Will load ``, if there is ``, it will load automatically// `` will overwrite the `` configuration$ CONFIGOR_ENV=production go test // Will load ``, if there is ``, it will load automatically// `` Will cover `` Configuration
// Set environment variables in the code(&{Environment: "production"}).Load(&Config, "")
Sample configuration
(&Config, "") $ go run # Will load automatically `` if `` If it does not exist, a warning message will be output
Output:
Failed to find configuration , using example file
{test {dodododo root 123 3306 } [{jack jack@} {tom tom@}]}
Loading configuration items from the shell
CONFIGOR_APPNAME="hello world" go run # The format is {{prefix}}_FieldName
# Overwrite prefix shell operation$ CONFIGOR_ENV_PREFIX="WEB" WEB_APPNAME="hello, prefix" go run # The code is written as(&{ENVPrefix: "WEB"}).Load(&Config, "")
Anonymous Struct
type Details Struct { Description string } type Config struct { Details `anonymous:"true"` }
If usinganonymous:"true"
Tags, thenDescription
The environment variable of the parameter will becomeCONFIGOR_DESCRIPTION
, otherwise the environment variable will beCONFIGOR_DETAILS_DESCRIPTION
This is the end of this article about the detailed explanation of the use of Golang Configor configuration file tool. For more related go configor content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!