欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

1.導入依賴包

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)用梅姐服務的ip,暫用當前,后續(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當中

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

相關文章

  • golang實現(xiàn)http服務器處理靜態(tài)文件示例

    golang實現(xiàn)http服務器處理靜態(tài)文件示例

    這篇文章主要介紹了golang實現(xiàn)http服務器處理靜態(tài)文件的方法,涉及Go語言基于http協(xié)議處理文件的相關技巧,需要的朋友可以參考下
    2016-07-07
  • golang讀取文件的常用方法總結(jié)

    golang讀取文件的常用方法總結(jié)

    今天小編就為大家分享一篇關于golang讀取文件的常用方法總結(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • Go 并發(fā)讀寫 sync.map 詳細

    Go 并發(fā)讀寫 sync.map 詳細

    閱讀本文你將會明確 sync.Map 和原生 map +互斥鎖/讀寫鎖之間的性能情況。標準庫 sync.Map 雖說支持并發(fā)讀寫 map,但更適用于讀多寫少的場景,因為他寫入的性能比較差,使用時要考慮清楚這一點。
    2021-10-10
  • go并發(fā)編程sync.Cond使用場景及實現(xiàn)原理

    go并發(fā)編程sync.Cond使用場景及實現(xiàn)原理

    這篇文章主要為大家介紹了go并發(fā)編程sync.Cond使用場景及實現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • go-micro微服務JWT跨域認證問題

    go-micro微服務JWT跨域認證問題

    JWT 以 JSON 對象的形式安全傳遞信息。因為存在數(shù)字簽名,因此所傳遞的信息是安全的,這篇文章主要介紹了go-micro微服務JWT跨域認證,需要的朋友可以參考下
    2023-01-01
  • Go 語言中 20 個占位符的整理

    Go 語言中 20 個占位符的整理

    這篇文章主要介紹了Go 語言中 20 個占位符的整理,看完本篇文章講學會什么是占位符?哪些函數(shù)支持?如何使用占位符?不同的占位符的作用?配合占位符的幾個標記符號用法?
    2021-10-10
  • 在Go程序中實現(xiàn)服務器重啟的方法

    在Go程序中實現(xiàn)服務器重啟的方法

    這篇文章主要介紹了在Go程序中實現(xiàn)服務器重啟的方法,由于很多人盲目崇拜谷歌"親爹",Go語言在國內(nèi)有著不尋常的人氣,需要的朋友可以參考下
    2015-06-06
  • golang如何通過type定義函數(shù)類型

    golang如何通過type定義函數(shù)類型

    這篇文章主要介紹了golang如何通過type定義函數(shù)類型問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Golang實現(xiàn)WebSocket服務的項目實踐

    Golang實現(xiàn)WebSocket服務的項目實踐

    本文介紹如何使用Golang實現(xiàn)實時后端WebSocket服務,首先使用Gin框架搭建http服務,然后使用gorilla/websocket庫實現(xiàn)簡單后端WebSocket服務,具有一定的參考價值,感興趣的可以了解一下
    2023-05-05
  • Go Gin中間件的具體使用

    Go Gin中間件的具體使用

    中間件是Gin框架中的一個重要概念,它可以用來處理HTTP請求和響應,或者在處理請求之前和之后執(zhí)行一些操作,本文主要介紹了Go Gin中間件的具體使用,感興趣的可以了解一下
    2023-11-11

最新評論