This is a creation in Article, where the information may have evolved or changed.
Code
Https://github.com/fengchunjian/goexamples/viper
//main.gopackage mainimport ("FMT" "Github.com/spf13/viper" "OS" "strings") const CMDROOT = "core" F UNC main () {viper. Setenvprefix (cmdroot) Viper. Automaticenv () Replacer: = Strings. Newreplacer (".", "_") Viper. Setenvkeyreplacer (replacer) Viper. Setconfigname (cmdroot) Viper. Addconfigpath ("./") Err: = Viper. Readinconfig () if err! = Nil {fmt. Println (FMT. Errorf ("Fatal error when reading%s config file:%s", cmdroot, Err)) OS. Exit (1)} Environment: = Viper. Getbool ("security.enabled") fmt. Println ("security.enabled:", environment) Fullstate: = Viper. GetString ("Statetransfer.timeout.fullstate") fmt. Println ("Statetransfer.timeout.fullstate:", fullstate) Abcdvalue: = Viper. GetString ("PEER.ABCD") fmt. Println ("PEER.ABCD:", Abcdvalue)}
//core.yamlstatetransfer: recoverdamage: true blocksperrequest: 20 maxdeltas: 200 timeout: singleblock: 2s singlestatedelta: 2s fullstate: 60speer: abcd: 3322d
Compile
Go get Github.com/spf13/viper
Go build-o VIP
Handling the Go get github.com/spf13/viper process issues
Unrecognized import path "Golang.org/x/sys/unix"
Unrecognized import path "Golang.org/x/text/transform"
Unrecognized import path "Golang.org/x/text/unicode/norm"
Workaround:
git clone https://github.com/golang/sys.git $GOPATH/src/golang.org/x/sys
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
Run
CORE_SECURITY_ENABLED=true ./vipsecurity.enabled: truestatetransfer.timeout.fullstate: 60speer.abcd: 3322d
Reference documents
Golang plug-in Viper
http://blog.csdn.net/qq_27809391/article/details/54091977