Spf13 Viper code example

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.