golang怎么通过viper读取config.yaml文件
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,这篇文章主要讲解了"golang怎么通过viper读取config.yaml文件",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"golang怎么通过vi
千家信息网最后更新 2025年02月04日golang怎么通过viper读取config.yaml文件
这篇文章主要讲解了"golang怎么通过viper读取config.yaml文件",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"golang怎么通过viper读取config.yaml文件"吧!
1.导入依赖包
import ( "github.com/spf13/viper")
2.编写yaml文件
放在conf
目录下,文件名叫config.yaml
# TODO 本地调试时放开KubeSphere_URL: http://192.168.103.48:3188# TODO 部署到环境时放开#KubeSphere_URL: http://ks-apiserver.kubesphere-system.svc:80KubesphereAdminUser: adminKubespherePassword: Admin123#TODO 调用梅姐服务的ip,暂用当前,后续需要修改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:8093Other_service_URL: /capis/quota.ictnj.io/v1alpha1/namespaces/#TODO harbor镜像仓库地址HARBOR_URL: https://192.168.66.4:443HARBOR_ADMIN_USERNAME: adminHARBOR_ADMIN_PASSWD: Harbor12345HARBOR_IP_HTTPS: 192.168.66.4:443HARBOR_SSH_ADDRESS: 192.168.103.48:53304HARBOR_SSH_USERNAME: rootHARBOR_SSH_PASSWD: peng123.
3.编写读取yaml文件的go文件
放在config目录下,文件名叫config.go
需要注意的是目录的问题,如果放在同目录,直接用configurationPath
,不同的编辑器,
vscode跟golang对相对路径处理不同
package configimport ( "github.com/spf13/viper")const ( configurationName = "config" configurationPath = "./conf" // vscode特殊读取路径 // configurationPath_vscode = "../conf" )var Config *viper.Viperfunc 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 configimport ( "github.com/spf13/viper")const ( configurationName = "config" configurationPath = "./conf" // vscode特殊读取路径 configurationPath_vscode = "../conf" )var Config *viper.Viperfunc 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怎么通过viper读取config.yaml文件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
文件
路径
不同
目录
学习
特殊
内容
对象
问题
仓库
同事
地址
就是
思路
情况
文章
更多
源码
环境
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
查询数据库多少张表
方舟进化专用服务器
深圳市夭洛网络技术有限公司
数据库应用技术实训报告心得
时间核对数据库
北京crm软件开发流程
我的世界服务器流量
移动办公怎么查看服务器地址
数据库管理员英文缩写
软件开发中各种图
计算机网络技术的薪资待遇
软件开发方向学习什么
南京计算机开发软件开发
服务器共享文件管理破解软件
江苏服务器供应商
网络技术学校排名
光明新区定制软件开发
原神更新服务器异常
齐鲁工惠网络安全周
居委会网络安全周宣传活动简报
大兴区品牌软件开发程序
服务器地址非法
模糊查询数据库姓名
从滴滴看网络安全
sr650服务器网口管理口
数据库链接池在哪设置
深圳程序软件开发定做
南京安卓软件开发公司哪家好
东华软件开发
w7虚拟机服务器管理器