Go?viper讀取配置文件的示例詳解
參考
安裝
go get github.com/spf13/viper
簡單例子
/* 目錄 - main.go - config.yaml */ func main(){ viper.SetConfigName("config") // 設(shè)置配置名稱 viper.SetConfigFile("config.yaml") // 設(shè)置配置文件路徑 // 讀取配置 if err := viper.ReadInConfig(); err != nil{ log.Fatal(err) } // 獲取配置數(shù)據(jù) summary := viper.GetString("Summary") fmt.Println(summary) }
新建 Viper
viper 提供默認 Viper對象, 可直接使用。 也通過 New
方法創(chuàng)建自定義Viper
// 直接使用默認對象 viper.GetInt("count") // 獲取全局Viper對象 globalViper := viper.GetViper() // 新建Viper conf := viper.New() conf.SetConfigFile("config.yaml")
讀取配置文件
從配置讀取
viper.SetConfigName("config") viper.SetConfigFile("config.yaml") if err := viper.ReadInConfig(); err != nil{ log.Fatal(err) } version := viper.GetString("version")
從 io.Reader 讀取
func main(){ file, err := os.Open("./config.yaml") if err != nil{ log.Fatal(err) } defer file.Close() viper.SetConfig("yaml") version := viper.GetString("version") // 這里需要設(shè)置配置類型, 否則無法正確解釋配置內(nèi)容 // 如果未配置類型,也可以通過 Get 方法獲取數(shù)據(jù)自行解析。 }
從 flag 讀取
import ( "fmt" "github.com/spf13/pflag" "github.com/spf13/viper" ) func main() { pflag.String("ip", "127.0.0.1", "Server running address") pflag.Int64("port", 8080, "Server running port") pflag.Parse() viper.BindPFlags(pflag.CommandLine) fmt.Printf("ip :%s , port:%s", viper.GetString("ip"), viper.GetString("port")) }
獲取值
值得獲取方法名為 Get[type] 形式
接口類型,既未作解析
Get
布爾
GetBool
時間
- GetDuration
- GetTime
數(shù)字
- GetInt
- GetInt32
- GetInt64
- GetFloat64
- GetIntSlice
- ....
字符
- GetString
- GetStringSlice
- ....
設(shè)置默認值
setDefault
viper.setConfigFile("config.yaml") viper.SetDefault("port", 8000) println(viper.GetInt("port") // 8000 // 這里未讀取配置文件,將返回默認值
將配置映射到結(jié)構(gòu)體
Unmarshal 將配置屬性映射到 struct 中, 匹配模式類似 JSON 解析,只匹配大寫開頭的屬性
type User struct{ ID string Name string nickName string // 小寫屬性將不做匹配 } func main(){ /* config.yaml id: id11 name: Rogan nickName: "wolf" */ ... user := &User{} viper.Unmarshal(user) println(&user) // { id11, Rogan } }
UnmarshalExact 用法與 Unmarshal 相同, 不同的是struct 必須與配置屬性完全匹配,否則加報錯
/* config.yaml id: 111 name: Rogan */ type Conf_1 struct{ Id string Name string } // 將報錯 type Conf_2 struct{ Id string }
UnmarshalKey 匹配某一字段
/* config.yaml server: port: 8000 host: localhost */ type ServerConf struct{ Port int host string } func main(){ ... serverConf := &ServerConf{} viper.UnmarshalKey("server", serverConfig) }
混合配置 Merge[type]
MergeConfig
func main(){ viper.SetConfigName("config") viper.SetConfigFile("config.yaml") viper.ReadInConfig() log.Println(viper.GetString("Summary"), viper.InConfig("Summary")) buff := []byte(`Name: jeck`) viper.MergeConfig(bytes.NewReader(buff)) log.Println(viper.GetString("Name")) }
監(jiān)聽文件
WatchConfig
func main(){ viper.SetConfigName("config") viper.SetConfigFile("config.yaml") viper.ReadInConfig() viper.WatchConfig() for { log.Println(viper.GetString("version")) time.Sleep(time.Second * 5) } } // 初始 versioni: 1 // --> 1 // 修改config.yaml version: 2 // --> 2
OnConfigChange 響應配置變化
以上就是Go viper讀取配置文件的示例詳解的詳細內(nèi)容,更多關(guān)于go viper的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
golang validator參數(shù)校驗的實現(xiàn)
這篇文章主要介紹了golang validator參數(shù)校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10Golang中基于HTTP協(xié)議的網(wǎng)絡(luò)服務(wù)
HTTP協(xié)議是基于TCP/IP協(xié)議棧的,并且它也是一個面向普通文本的協(xié)議。這篇文章主要詳細介紹了Golang中基于HTTP協(xié)議的網(wǎng)絡(luò)服務(wù),感興趣的小伙伴可以借鑒一下2023-04-04go從指定的URL下載圖片并保存到本地的代碼實現(xiàn)
這段代碼定義了一個名為 downloadImage 的函數(shù),其目的是從指定的URL下載圖片并保存到本地文件系統(tǒng),本文是對代碼功能的詳細描述,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-08-08