golang如何通过viper读取config.yaml文件
作者:峰啊疯了 发布时间:2023-07-22 05:46:11
标签: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:80
KubesphereAdminUser: admin
KubespherePassword: 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: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当中
来源:https://blog.51cto.com/u_12040959/5098392


猜你喜欢
- #HelloWorld是文件名称,Hello是类from HelloWorld import Hello调用,Hello类的方法:>&
- 历史:Message Queue的需求由来已久,80年代最早在金融交易中,高盛等公司采用Teknekron公司的产品,当时的Message
- SQL Server创建临时表:创建临时表 方法一: &n
- 首先创建scrapy项目命令:scrapy startproject douban_read创建spider命令:scrapy genspi
- 为了方便各位朋友,本文收集了一些对Web开发人员非常有用的手册,记得推荐一下哦。HTML 速查手册HTML/XTML in one page
- 备忘一下python中的字典如何遍历,没有什么太多技术含量.仅供作为初学者的我参考.#!/usr/bin/env python# codin
- 1. 简介在 Go 语言中,new 和 make 是用于创建对象的两个内建函数,它们的使用方式和作用有所不同。正确理解 new 和 make
- 前言本文提供将视频按照时间维度进行剪切的工具方法,一如既往的实用主义。主要也是学习一下golang使用ffmpeg工具的方式。环境依赖ffm
- 在日常工作中,PPT制作是常见的工作,如果制作创意类PPT,则无法通过自动化的形式生成,因为创意本身具有随机性,而自动化解决的是重复性工作,
- 有些时候我们发现一些模块没有提供pip install 命令和安装教程 , 只提供了一个setup.py文件 , 这个时候如何安装呢?步骤打
- 人一旦习惯了某些东西就很难去改,以及各种各样的原因,新的浏览器越来越多,而老的总淘汰不了。增长总是快于消亡导致了浏览器兼容是成了谈不完的话题
- 很多的朋友一而再,再而三的在Server.Mappath上卡壳,cnbruce也是一遍两遍地重复,还是不能全部解决,所以通过下面的举例,希望
- 复制目录: 包含多层子目录方法: 递归, 深度遍历,广度遍历深度遍历&广度遍历:思路:1.获得源目录子级目录,并设置目标目录的子级路
- 本文通过Python3+pyqt5实现了python Qt GUI 快速编程的19章的页面索引器应用程序例子。/home/yrd/eric_
- 为了能让PHP连接MSSQL,系统需要安装MSSQL,PHP,且在PHP.ini中的配置中,将 ;extension=
- Python是一门简单易学的编程语言,语法简洁而清晰,并且拥有丰富和强大的类库。与其它大多数程序设计语言使用大括号不一样 ,它使用缩进来定义
- 查看Django版本检查是否安装成功,可以在dos下查看Django版本。1.输入python 2.输入import django3.输入d
- 作者:Roland Smart原文链接:http://www.adaptivepath.com/ideas/newsletter/archi
- 1.启动数据库命令行客户端#linux命令,注意区分大小写mysql2.查询数据库#执行结果:返回所有数据库列表SHOW DATABASES
- 问题:由于自己做项目的时候,需要循环的绘制数据,假设有100个样本,每个样本包含两个坐标点(A, B),我需要对这两个点标上不同的颜色,同时