golang如何通過(guò)viper讀取config.yaml文件
1.導(dǎo)入依賴(lài)包
import ( ? ? "github.com/spf13/viper" )
2.編寫(xiě)yaml文件
放在conf
目錄下,文件名叫config.yaml
# TODO ?本地調(diào)試時(shí)放開(kāi) KubeSphere_URL: http://192.168.103.48:3188 # TODO 部署到環(huán)境時(shí)放開(kāi) #KubeSphere_URL: http://ks-apiserver.kubesphere-system.svc:80 KubesphereAdminUser: admin KubespherePassword: Admin123 #TODO 調(diào)用梅姐服務(wù)的ip,暫用當(dāng)前,后續(xù)需要修改 Other_service_IP: http://192.168.103.48:30412 #Other_service_IP: http://container-cloud-system-controller-manager-metrics-service.container-cloud-system-system.svc:8093 Other_service_URL: /capis/quota.ictnj.io/v1alpha1/namespaces/ #TODO harbor鏡像倉(cāng)庫(kù)地址 HARBOR_URL: https://192.168.66.4:443 HARBOR_ADMIN_USERNAME: admin HARBOR_ADMIN_PASSWD: Harbor12345 HARBOR_IP_HTTPS: 192.168.66.4:443 HARBOR_SSH_ADDRESS: 192.168.103.48:53304 HARBOR_SSH_USERNAME: root HARBOR_SSH_PASSWD: peng123.
3.編寫(xiě)讀取yaml文件的go文件
放在config目錄下,文件名叫config.go
需要注意的是目錄的問(wèn)題,如果放在同目錄,直接用configurationPath
,不同的編輯器,
vscode跟golang對(duì)相對(duì)路徑處理不同
package config import ( ? ? "github.com/spf13/viper" ) const ( ? ? configurationName = "config" ? ? configurationPath = "./conf" ? ? // vscode特殊讀取路徑 ? // ?configurationPath_vscode = "../conf"? ) var Config *viper.Viper func init() { ? ? Config = viper.New() ? ? Config.SetConfigName(configurationName) ? ? Config.AddConfigPath(configurationPath) ? ? Config.SetConfigType("yaml") ? ? Config.AddConfigPath(configurationPath) ? ? if err := config.ReadInConfig(); err != nil { ? ? ?panic(err) ? ?}? }
如果config.yaml
跟config.go放在同目錄簡(jiǎn)單的路徑用上面這個(gè),如果路徑不同,且不同的同事用不同的編譯軟件,可以嘗試下面的路徑兼容
package config import ( ? ? "github.com/spf13/viper" ) const ( ? ? configurationName = "config" ? ? configurationPath = "./conf" ? ? // vscode特殊讀取路徑 ? ? configurationPath_vscode = "../conf"? ) var Config *viper.Viper func init() { ? ? Config = viper.New() ? ? Config.SetConfigName(configurationName) ? ? Config.AddConfigPath(configurationPath) ? ? Config.SetConfigType("yaml") ? ? if err := Config.ReadInConfig(); err != nil { ? ? ? ? Config.AddConfigPath(configurationPath_vscode) ? ? ? ? if err := Config.ReadInConfig(); err != nil { ? ? ? ? ? ? Config.AddConfigPath(configurationPath) ? ? ? ? ? ? panic(err) ? ? ? ? } ? ? } }
4.使用config對(duì)象
Config.GetString("KubeSphere_URL")
5.viper源碼分析
type Viper struct { ? ? // Delimiter that separates a list of keys ? ? // used to access a nested value in one go ? ? keyDelim string ? ? // A set of paths to look for the config file in ? ? configPaths []string ? ? // The filesystem to read config from. ? ? fs afero.Fs ? ? // A set of remote providers to search for the configuration ? ? remoteProviders []*defaultRemoteProvider ? ? // Name of file to look for inside the path ? ? configName ? ? ? ?string ? ? configFile ? ? ? ?string ? ? configType ? ? ? ?string ? ? configPermissions os.FileMode ? ? envPrefix ? ? ? ? string ? ? automaticEnvApplied bool ? ? envKeyReplacer ? ? ?StringReplacer ? ? allowEmptyEnv ? ? ? bool ? ? config ? ? ? ? map[string]interface{} ? ? override ? ? ? map[string]interface{} ? ? defaults ? ? ? map[string]interface{} ? ? kvstore ? ? ? ?map[string]interface{} ? ? pflags ? ? ? ? map[string]FlagValue ? ? env ? ? ? ? ? ?map[string]string ? ? aliases ? ? ? ?map[string]string ? ? typeByDefValue bool ? ? // Store read properties on the object so that we can write back in order with comments. ? ? // This will only be used if the configuration read is a properties file. ? ? properties *properties.Properties ? ? onConfigChange func(fsnotify.Event) }
func (v *Viper) ReadInConfig() error { ? ? jww.INFO.Println("Attempting to read in config file") ? ? filename, err := v.getConfigFile() ? ? if err != nil { ? ? ? ? return err ? ? } ? ? if !stringInSlice(v.getConfigType(), SupportedExts) { ? ? ? ? return UnsupportedConfigError(v.getConfigType()) ? ? } ? ? jww.DEBUG.Println("Reading file: ", filename) ? ? file, err := afero.ReadFile(v.fs, filename) ? ? if err != nil { ? ? ? ? return err ? ? } ? ? config := make(map[string]interface{}) ? ? err = v.unmarshalReader(bytes.NewReader(file), config) ? ? if err != nil { ? ? ? ? return err ? ? } ? ? v.config = config ? ? return nil }
把yaml文件的鍵值讀取到viper對(duì)象的config當(dāng)中
到此這篇關(guān)于golang如何通過(guò)viper讀取config.yaml文件的文章就介紹到這了,更多相關(guān)golang讀取config.yaml內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang實(shí)現(xiàn)http服務(wù)器處理靜態(tài)文件示例
這篇文章主要介紹了golang實(shí)現(xiàn)http服務(wù)器處理靜態(tài)文件的方法,涉及Go語(yǔ)言基于http協(xié)議處理文件的相關(guān)技巧,需要的朋友可以參考下2016-07-07Go 并發(fā)讀寫(xiě) sync.map 詳細(xì)
閱讀本文你將會(huì)明確 sync.Map 和原生 map +互斥鎖/讀寫(xiě)鎖之間的性能情況。標(biāo)準(zhǔn)庫(kù) sync.Map 雖說(shuō)支持并發(fā)讀寫(xiě) map,但更適用于讀多寫(xiě)少的場(chǎng)景,因?yàn)樗麑?xiě)入的性能比較差,使用時(shí)要考慮清楚這一點(diǎn)。2021-10-10go并發(fā)編程sync.Cond使用場(chǎng)景及實(shí)現(xiàn)原理
這篇文章主要為大家介紹了go并發(fā)編程sync.Cond使用場(chǎng)景及實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08go-micro微服務(wù)JWT跨域認(rèn)證問(wèn)題
JWT 以 JSON 對(duì)象的形式安全傳遞信息。因?yàn)榇嬖跀?shù)字簽名,因此所傳遞的信息是安全的,這篇文章主要介紹了go-micro微服務(wù)JWT跨域認(rèn)證,需要的朋友可以參考下2023-01-01在Go程序中實(shí)現(xiàn)服務(wù)器重啟的方法
這篇文章主要介紹了在Go程序中實(shí)現(xiàn)服務(wù)器重啟的方法,由于很多人盲目崇拜谷歌"親爹",Go語(yǔ)言在國(guó)內(nèi)有著不尋常的人氣,需要的朋友可以參考下2015-06-06golang如何通過(guò)type定義函數(shù)類(lèi)型
這篇文章主要介紹了golang如何通過(guò)type定義函數(shù)類(lèi)型問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Golang實(shí)現(xiàn)WebSocket服務(wù)的項(xiàng)目實(shí)踐
本文介紹如何使用Golang實(shí)現(xiàn)實(shí)時(shí)后端WebSocket服務(wù),首先使用Gin框架搭建http服務(wù),然后使用gorilla/websocket庫(kù)實(shí)現(xiàn)簡(jiǎn)單后端WebSocket服務(wù),具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05