viper 支援Yaml、Json、 TOML、HCL 等格式,讀取非常的方便。
go get github.com/spf13/viper
如果提示找不到golang.org/x/text/這個函式庫,是因為golang.org/x/text/這個函式庫在GitHub上託管的路徑不一致。
解決方法:
可從https://github.com/golang/text下載原始碼下來,然後到$GOPATH/src下面建立golang.org/x/資料夾(已存在的忽略),把壓縮包的檔案解壓縮到golang.org/x/資料夾之下。
接著執行 go install -x golang.org/x/text 即可解決:
初始結構目錄如下:
準備測試中使用的yaml文件,注意yaml的格式十分嚴格,主要是每個冒號後面必須要有空格,數組前要加“-”號表示連續(注意減號後面也有空格),內容如下:
TimeStamp: "2018-10-18 10:09:23" Address: "Shenzhen" Postcode: 518000 CompanyInfomation: Name: "Sunny" MarketCapitalization: 50000000 EmployeeNum: 200 Department: - "Finance" - "Design" - "Program" - "Sales" IsOpen: false
package main import ( "github.com/spf13/viper" "fmt" ) func main() { //读取yaml文件 v := viper.New() //设置读取的配置文件 v.SetConfigName("linux_config") //添加读取的配置文件路径 v.AddConfigPath("./config/") //windows环境下为%GOPATH,linux环境下为$GOPATH v.AddConfigPath("$GOPATH/src/") //设置配置文件类型 v.SetConfigType("yaml") if err := v.ReadInConfig();err != nil { fmt.Printf("err:%s\n",err) } fmt.Printf( ` TimeStamp:%s CompanyInfomation.Name:%s CompanyInfomation.Department:%s `, v.Get("TimeStamp"), v.Get("CompanyInfomation.Name"), v.Get("CompanyInfomation.Department"), ) /* result: TimeStamp:2018-10-18 10:09:23 CompanyInfomation.Name:Sunny CompanyInfomation.Department:[Finance Design Program Sales] */ }
package main import ( "github.com/spf13/viper" "fmt" ) func main() { //读取yaml文件 v := viper.New() //设置读取的配置文件 v.SetConfigName("linux_config") //添加读取的配置文件路径 v.AddConfigPath("./config/") //windows环境下为%GOPATH,linux环境下为$GOPATH v.AddConfigPath("$GOPATH/src/") //设置配置文件类型 v.SetConfigType("yaml") if err := v.ReadInConfig();err != nil { fmt.Printf("err:%s\n",err) } fmt.Printf( ` TimeStamp:%s CompanyInfomation.Name:%s CompanyInfomation.Department:%s `, v.Get("TimeStamp"), v.Get("CompanyInfomation.Name"), v.Get("CompanyInfomation.Department"), ) /* result: TimeStamp:2018-10-18 10:09:23 CompanyInfomation.Name:Sunny CompanyInfomation.Department:[Finance Design Program Sales] */ //反序列化 parseYaml(v) } type CompanyInfomation struct{ Name string MarketCapitalization int64 EmployeeNum int64 Department []interface{} IsOpen bool } type YamlSetting struct{ TimeStamp string Address string Postcode int64 CompanyInfomation CompanyInfomation } func parseYaml(v *viper.Viper){ var yamlObj YamlSetting; if err := v.Unmarshal(&yamlObj) ; err != nil{ fmt.Printf("err:%s",err) } fmt.Println(yamlObj) /* result: {2018-10-18 10:09:23 Shenzhen 518000 {Sunny 50000000 200 [Finance Design Program Sales] false}} */ }
package main import ( "github.com/spf13/pflag" "github.com/spf13/viper" "fmt" ) func main() { pflag.String("hostAddress", "127.0.0.1", "Server running address") pflag.Int64("port", 8080, "Server running port") pflag.Parse() viper.BindPFlags(pflag.CommandLine) fmt.Printf("hostAddress :%s , port:%s", viper.GetString("hostAddress"), viper.GetString("port")) /* example: go run main2.go --hostAddress=192.192.1.10 --port=9000 help: Usage of /tmp/go-build183981952/b001/exe/main: --hostAddress string Server running address (default "127.0.0.1") --port int Server running port (default 8080) */ }
package main import ( "github.com/spf13/viper" "fmt" "golang.org/x/net/context" "github.com/fsnotify/fsnotify" ) func main() { //读取yaml文件 v := viper.New() //设置读取的配置文件 v.SetConfigName("linux_config") //添加读取的配置文件路径 v.AddConfigPath("./config/") //windows环境下为%GOPATH,linux环境下为$GOPATH v.AddConfigPath("$GOPATH/src/") //设置配置文件类型 v.SetConfigType("yaml") if err := v.ReadInConfig(); err != nil { fmt.Printf("err:%s\n", err) } //创建一个信道等待关闭(模拟服务器环境) ctx, _ := context.WithCancel(context.Background()) //cancel可以关闭信道 //ctx, cancel := context.WithCancel(context.Background()) //设置监听回调函数 v.OnConfigChange(func(e fsnotify.Event) { fmt.Printf("config is change :%s \n", e.String()) //cancel() }) //开始监听 v.WatchConfig() //信道不会主动关闭,可以主动调用cancel关闭 <-ctx.Done() /* result: config is change :"/home/share/go/Viper/config/linux_config.yaml": CREATE config is change :"/home/share/go/Viper/config/linux_config.yaml": CREATE */ }
完結 viper還有許多好用的功能,這篇文章只是舉例說明了很小的部分,歡迎留言,多提提意見,感謝大家。
更多golang相關技術文章,請造訪golang#######教學欄位! ###### ###以上是分享Golang中一個好用的viper配置模組的詳細內容。更多資訊請關注PHP中文網其他相關文章!