Go讀取yaml文件到struct類的實(shí)現(xiàn)方法
1、yaml文件準(zhǔn)備
common: secretid: AKIDxxxxx secretKey: 3xgGxxxx egion: ap-guangzhou zone: ap-guangzhou-7 InstanceChargeType: POSTPAID_BY_HOUR
2、config配置類準(zhǔn)備
可以通過(guò)在線配置工具轉(zhuǎn)換成struct
例如:https://www.printlove.cn/tools/yaml2go

代碼:
type ConfigData struct {
// 公共配置
Common Common `yaml:"common"`
}
type Common struct {
// 密鑰id。密鑰可前往官網(wǎng)控制臺(tái) https://console.cloud.tencent.com/cam/capi 進(jìn)行獲取
SecretId string `yaml:"secretid"`
// 密鑰key
SecretKey string `yaml:"secretKey"`
// 地域
Region string `yaml:"region"`
// 可用區(qū)
Zone string `yaml:"zone"`
//實(shí)例計(jì)費(fèi)模式。取值范圍:PREPAID:預(yù)付費(fèi),即包年包月。POSTPAID_BY_HOUR:按小時(shí)后付費(fèi)。
InstanceChargeType string `yaml:"InstanceChargeType"`
}
3、讀取配置文件到配置類
使用viper讀取配置到配置類中
3.1、安裝Viper組件
go install github.com/spf13/viper@latest
3.2、golang** **代碼編寫
yaml文件放在工程根目錄的data文件夾中
package main
import (
"bufio"
"github.com/spf13/viper"
"io"
"os"
"strings"
)
type ConfigData struct {
// 公共配置
Common Common `yaml:"common"`
}
type Common struct {
// 密鑰id。
SecretId string `yaml:"secretid"`
// 密鑰key
SecretKey string `yaml:"secretKey"`
// 地域
Region string `yaml:"region"`
// 可用區(qū)
Zone string `yaml:"zone"`
//實(shí)例計(jì)費(fèi)模式。取值范圍:PREPAID:預(yù)付費(fèi),即包年包月。POSTPAID_BY_HOUR:按小時(shí)后付費(fèi)。
InstanceChargeType string `yaml:"InstanceChargeType"`
}
func InitConfigStruct(path string) *ConfigData {
var ConfigData = &ConfigData{}
vip := viper.New()
vip.AddConfigPath(path)
vip.SetConfigName("config")
vip.SetConfigType("yaml")
//嘗試進(jìn)行配置讀取
if err := vip.ReadInConfig(); err != nil {
panic(err)
}
err := vip.Unmarshal(ConfigData)
if err != nil {
panic(err)
}
return ConfigData
}
func main(){
configData := InitConfigStruct("./data/")
secretId := configData.Common.SecretId
secretKey := configData.Common.SecretKey
fmt.Printf("secretId:%s\n", secretId)
fmt.Printf("secretKey:%s\n", secretKey)
}| 作者:周欽雄 出處:http://www.cnblogs.com/zhouqinxiong/ |
到此這篇關(guān)于Go讀取yaml文件到struct類的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Go讀取yaml文件 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang調(diào)用FFmpeg轉(zhuǎn)換視頻流的實(shí)現(xiàn)
本文主要介紹了Golang調(diào)用FFmpeg轉(zhuǎn)換視頻流,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Golang使用Zookeeper實(shí)現(xiàn)分布式鎖
分布式鎖是一種在分布式系統(tǒng)中用于控制并發(fā)訪問(wèn)的機(jī)制,ZooKeeper?和?Redis?都是常用的實(shí)現(xiàn)分布式鎖的工具,本文就來(lái)使用Zookeeper實(shí)現(xiàn)分布式鎖,希望對(duì)大家有所幫助2024-02-02
利用Go語(yǔ)言實(shí)現(xiàn)Raft日志同步
這篇文章主要為大家詳細(xì)介紹了如何利用Go語(yǔ)言實(shí)現(xiàn)Raft日志同步,文中的示例代碼講解詳細(xì),對(duì)我們深入了解Go語(yǔ)言有一定的幫助,需要的可以參考一下2023-05-05
詳解golang中發(fā)送http請(qǐng)求的幾種常見(jiàn)情況
這篇文章主要介紹了詳解golang中發(fā)送http請(qǐng)求的幾種常見(jiàn)情況,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
部署Go語(yǔ)言項(xiàng)目的 N 種方法(小結(jié))
這篇文章主要介紹了部署Go語(yǔ)言項(xiàng)目的 N 種方法(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

