最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

golang如何通過viper讀取config.yaml文件

 更新時間:2022年03月16日 17:16:33   作者:峰啊瘋了  
這篇文章主要介紹了golang通過viper讀取config.yaml文件,圍繞golang讀取config.yaml文件的相關(guān)資料展開詳細(xì)內(nèi)容,需要的小伙伴可以參考一下

1.導(dǎo)入依賴包

import (
? ? "github.com/spf13/viper"
)

2.編寫yaml文件

放在conf目錄下,文件名叫config.yaml

# TODO ?本地調(diào)試時放開
KubeSphere_URL: http://192.168.103.48:3188
# TODO 部署到環(huán)境時放開
#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鏡像倉庫地址
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.編寫讀取yaml文件的go文件

放在config目錄下,文件名叫config.go

需要注意的是目錄的問題,如果放在同目錄,直接用configurationPath,不同的編輯器,

vscode跟golang對相對路徑處理不同

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放在同目錄簡單的路徑用上面這個,如果路徑不同,且不同的同事用不同的編譯軟件,可以嘗試下面的路徑兼容

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對象

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對象的config當(dāng)中

到此這篇關(guān)于golang如何通過viper讀取config.yaml文件的文章就介紹到這了,更多相關(guān)golang讀取config.yaml內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go語言編程中判斷文件是否存在是創(chuàng)建目錄的方法

    Go語言編程中判斷文件是否存在是創(chuàng)建目錄的方法

    這篇文章主要介紹了Go語言編程中判斷文件是否存在是創(chuàng)建目錄的方法,示例都是使用os包下的函數(shù),需要的朋友可以參考下
    2015-10-10
  • Go語言入門學(xué)習(xí)之Channel通道詳解

    Go語言入門學(xué)習(xí)之Channel通道詳解

    go routine可以使用channel來進(jìn)行通信,使用通信的手段來共享內(nèi)存,下面這篇文章主要給大家介紹了關(guān)于Go語言入門學(xué)習(xí)之Channel通道的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • Win10系統(tǒng)下Golang環(huán)境搭建全過程

    Win10系統(tǒng)下Golang環(huán)境搭建全過程

    在編程語言的選取上,越來越多的人選擇了Golang,下面這篇文章主要給大家介紹了關(guān)于Win10系統(tǒng)下Golang環(huán)境搭建的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • Go中的go.mod使用詳解

    Go中的go.mod使用詳解

    這篇文章主要介紹了Go中的go.mod使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 解決Golang并發(fā)工具Singleflight的問題

    解決Golang并發(fā)工具Singleflight的問題

    前段時間在一個項目里使用到了分布式鎖進(jìn)行共享資源的訪問限制,后來了解到Golang里還能夠使用singleflight對共享資源的訪問做限制,于是利用空余時間了解,將知識沉淀下來,并做分享
    2022-05-05
  • go語言Timer計時器的用法示例詳解

    go語言Timer計時器的用法示例詳解

    Go語言的標(biāo)準(zhǔn)庫里提供兩種類型的計時器Timer和Ticker。這篇文章通過實例代碼給大家介紹go語言Timer計時器的用法,代碼簡單易懂,感興趣的朋友跟隨小編一起看看吧
    2020-05-05
  • Go中的 panic / recover 簡介與實踐記錄

    Go中的 panic / recover 簡介與實踐記錄

    這篇文章主要介紹了Go中的 panic / recover 簡介與實踐,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • go sync Once實現(xiàn)原理示例解析

    go sync Once實現(xiàn)原理示例解析

    這篇文章主要為大家介紹了go sync Once實現(xiàn)原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • 使用systemd部署和守護(hù)golang應(yīng)用程序的操作方法

    使用systemd部署和守護(hù)golang應(yīng)用程序的操作方法

    systemd是一個流行的守護(hù)進(jìn)程管理器,可以輕松管理服務(wù)的啟動、停止、重啟等操作,讓我們的應(yīng)用程序始終保持在線,本文介紹了如何使用systemd部署和守護(hù)golang應(yīng)用程序,感興趣的朋友一起看看吧
    2023-10-10
  • golang使用redis實現(xiàn)全文搜索功能詳解

    golang使用redis實現(xiàn)全文搜索功能詳解

    這篇文章主要為大家詳細(xì)介紹了golang如何使用redis實現(xiàn)全文搜索功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-02-02

最新評論

鹤岗市| 恩施市| 枞阳县| 抚州市| 威海市| 富阳市| 伊金霍洛旗| 南部县| 广东省| 吉林市| 宁河县| 宁明县| 梁山县| 大城县| 文登市| 二手房| 福泉市| 富阳市| 宣城市| 溆浦县| 阳谷县| 萨迦县| 淄博市| 玉树县| 高雄市| 锦州市| 江西省| 枣阳市| 中牟县| 黄骅市| 古蔺县| 雷波县| 昌平区| 马边| 香港 | 永靖县| 星子县| 东丽区| 周口市| 宁德市| 彰武县|