Golang使用第三方包viper讀取yaml配置信息操作
Golang有很多第三方包,其中的 viper 支持讀取多種配置文件信息。本文只是做一個小小demo,用來學(xué)習(xí)入門用的。
1、安裝
go get github.com/spf13/viper
2、編寫一個yaml的配置文件,config.yaml
database: host: 127.0.0.1 user: root dbname: test pwd: 123456
3、編寫學(xué)習(xí)腳本main.go,讀取config.yaml配置信息
package main
import (
"fmt"
"os"
"github.com/spf13/viper"
)
func main() {
//獲取項(xiàng)目的執(zhí)行路徑
path, err := os.Getwd()
if err != nil {
panic(err)
}
config := viper.New()
config.AddConfigPath(path) //設(shè)置讀取的文件路徑
config.SetConfigName("config") //設(shè)置讀取的文件名
config.SetConfigType("yaml") //設(shè)置文件的類型
//嘗試進(jìn)行配置讀取
if err := config.ReadInConfig(); err != nil {
panic(err)
}
//打印文件讀取出來的內(nèi)容:
fmt.Println(config.Get("database.host"))
fmt.Println(config.Get("database.user"))
fmt.Println(config.Get("database.dbname"))
fmt.Println(config.Get("database.pwd"))
}
4、執(zhí)行g(shù)o run main.go
輸出:
127.0.0.1 root test 123456
ok!
補(bǔ)充:go基于viper實(shí)現(xiàn)配置文件熱更新及其源碼分析
go第三方庫 github.com/spf13/viper 實(shí)現(xiàn)了對配置文件的讀取并注入到結(jié)構(gòu)中,好用方便。
其中以
viperInstance := viper.New() // viper實(shí)例
viperInstance.WatchConfig()
viperInstance.OnConfigChange(func(e fsnotify.Event) {
log.Print("Config file updated.")
viperLoadConf(viperInstance) // 加載配置的方法
})
可實(shí)現(xiàn)配置的熱更新,不用重啟項(xiàng)目新配置即可生效(實(shí)現(xiàn)熱加載的方法也不止這一種,比如以文件的上次修改時間來判斷等)。
為什么這么寫?這樣寫為什么就能立即生效?基于這兩個問題一起來看看viper是怎樣實(shí)現(xiàn)熱更新的。
上面代碼的核心一共兩處:WatchConfig()方法、OnConfigChange()方法。WatchConfig()方法用來開啟事件監(jiān)聽,確定用戶操作文件后該文件是否可正常讀取,并將內(nèi)容注入到viper實(shí)例的config字段,先來看看WatchConfig()方法:
func (v *Viper) WatchConfig() {
go func() {
// 建立新的監(jiān)視處理程序,開啟一個協(xié)程開始等待事件
// 從I/O完成端口讀取,將事件注入到Event對象中:Watcher.Events
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way
filename, err := v.getConfigFile()
if err != nil {
log.Println("error:", err)
return
}
configFile := filepath.Clean(filename) //配置文件E:\etc\bizsvc\config.yml
configDir, _ := filepath.Split(configFile) // E:\etc\bizsvc\
done := make(chan bool)
go func() {
for {
select {
// 讀取的event對象有兩個屬性,Name為E:\etc\bizsvc\config.yml,Op為write(對文件的操作)
case event := <-watcher.Events:
// 清除內(nèi)部的..和他前面的元素,清除當(dāng)前路徑.,用來判斷操作的文件是否是configFile
if filepath.Clean(event.Name) == configFile {
// 如果對該文件進(jìn)行了創(chuàng)建操作或?qū)懖僮?
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
err := v.ReadInConfig()
if err != nil {
log.Println("error:", err)
}
v.onConfigChange(event)
}
}
case err := <-watcher.Errors:
// 有錯誤將打印
log.Println("error:", err)
}
}
}()
watcher.Add(configDir)
<-done
}()
}
其中,fsnotify是用來監(jiān)控目錄及文件的第三方庫; watcher, err := fsnotify.NewWatcher() 用來建立新的監(jiān)視處理程序,它會開啟一個協(xié)程開始等待讀取事件,完成 從I / O完成端口讀取任務(wù),將事件注入到Event對象中,即Watcher.Events;

執(zhí)行v.ReadInConfig()后配置文件的內(nèi)容將重新讀取到viper實(shí)例中,如下圖:

執(zhí)行完v.ReadInConfig()后,config字段的內(nèi)容已經(jīng)是用戶修改的最新內(nèi)容了;
其中這行v.onConfigChange(event)的onConfigChange是核心結(jié)構(gòu)體Viper的一個屬性,類型是func:
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
envPrefix string
automaticEnvApplied bool
envKeyReplacer *strings.Replacer
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)
}
它用來傳入本次event來執(zhí)行你寫的函數(shù)。為什么修改會立即生效?相信第二個疑問已經(jīng)得到解決了。
接下來看看OnConfigChange(func(e fsnotify.Event) {...... })的運(yùn)行情況:
func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
v.onConfigChange = run
}
方法參數(shù)為一個函數(shù),類型為func(in fsnotify.Event)) {},這就意味著開發(fā)者需要把你自己的執(zhí)行邏輯放到這個func里面,在監(jiān)聽到event時就會執(zhí)行你寫的函數(shù),所以就可以這樣寫:
viperInstance.OnConfigChange(func(e fsnotify.Event) {
log.Print("Config file updated.")
viperLoadConf(viperInstance) // viperLoadConf函數(shù)就是將最新配置注入到自定義結(jié)構(gòu)體對象的邏輯
})
而OnConfigChange方法的參數(shù)會賦值給形參run并傳到viper實(shí)例的onConfigChange屬性,以WatchConfig()方法中的v.onConfigChange(event)來執(zhí)行這個函數(shù)。
到此,第一個疑問也就解決了。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Go 代碼規(guī)范錯誤處理示例經(jīng)驗(yàn)總結(jié)
這篇文章主要為大家介紹了Go 代碼規(guī)范錯誤處理示例實(shí)戰(zhàn)經(jīng)驗(yàn)總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
詳解go-admin在線開發(fā)平臺學(xué)習(xí)(安裝、配置、啟動)
這篇文章主要介紹了go-admin在線開發(fā)平臺學(xué)習(xí)(安裝、配置、啟動),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
深入分析Golang Server源碼實(shí)現(xiàn)過程
這篇文章深入介紹了Golang Server源碼實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
Go語言設(shè)計模式之實(shí)現(xiàn)觀察者模式解決代碼臃腫
今天學(xué)習(xí)一下用?Go?實(shí)現(xiàn)觀察者模式,觀察者模式主要是用來實(shí)現(xiàn)事件驅(qū)動編程。事件驅(qū)動編程的應(yīng)用還是挺廣的,除了我們都知道的能夠用來解耦:用戶修改密碼后,給用戶發(fā)短信進(jìn)行風(fēng)險提示之類的典型場景,在微服務(wù)架構(gòu)實(shí)現(xiàn)最終一致性、實(shí)現(xiàn)事件源A?+?ES2022-08-08
Go語言之io.ReadAtLeast函數(shù)的基本使用和原理解析
io.ReadAtLeast函數(shù)是Go語言標(biāo)準(zhǔn)庫提供的一個工具函數(shù),能夠從數(shù)據(jù)源讀取至少指定數(shù)量的字節(jié)數(shù)據(jù)到緩沖區(qū)中,這篇文章主要介紹了io.ReadAtLeast函數(shù)的相關(guān)知識,需要的朋友可以參考下2023-07-07

