In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "golang how to read config.yaml files through viper", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "golang how to read config.yaml files through viper" bar!
1. Import the dependency package import ("github.com/spf13/viper") 2. Write yaml files
Put it in the conf directory and the file is called config.yaml
# TODO release KubeSphere_URL during local debugging: release http://192.168.103.48:3188# TODO when deployed to the environment # KubeSphere_URL: http://ks-apiserver.kubesphere-system.svc:80KubesphereAdminUser: adminKubespherePassword: Admin123#TODO calls the ip of Mei Jie service, temporarily using the current Later, you need to modify the address of 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 image repository 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. Write go files that read yaml files
Put it in the config directory and the file is called config.go
What you need to pay attention to is the catalog problem. If you put it in the same directory, use configurationPath directly, a different editor.
Vscode and golang handle relative paths differently.
Package configimport ("github.com/spf13/viper") const (configurationName = "config" configurationPath = ". / conf" / / vscode special read path / / 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)}}
If config.yaml and config.go are placed in the same directory as the simple path above, if the path is different and different colleagues use different compiler software, you can try the following path compatibility
Package configimport ("github.com/spf13/viper") const (configurationName = "config" configurationPath = ". / conf" / / vscode special read path 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. Working with config objects
Config.GetString ("KubeSphere_URL") 5.viper source code analysis 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}
Read the key value of the yaml file into the config of the viper object
Thank you for your reading, these are the contents of "how golang reads config.yaml files through viper". After the study of this article, I believe you have a deeper understanding of how golang reads config.yaml files through viper, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.