配置文件使用viper在GO中返回null值。

huangapple go评论111阅读模式
英文:

Configuration file using viper giving null value in GO

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是Go语言的新手,正在使用viper进行配置管理。

配置文件的格式如下(config.yaml):

  1. SD_ORIGIN: 网址
  2. API_TRACKING_ID: 77xxx1
  3. API_TOKEN: sdsaxxxdfds123

我使用以下Go代码来读取值,但在读取代码片段时得到了空值:

  1. viper.SetConfigName("config")
  2. viper.AddConfigPath(".")
  3. err := viper.ReadInConfig()
  4. if err != nil {
  5. fmt.Println("配置文件未找到...")
  6. }
  7. var apiTrackingID = viper.GetString("API_TRACKING_ID")

请帮助我找出配置文件或Go实现中的问题。

英文:

i am new in Go and using viper for configuration management

configuration file looks like (config.yaml)

  1. SD_ORIGIN : web url
  2. API_TRACKING_ID : 77xxx1
  3. API_TOKEN : sdsaxxxdfds123

And using the go code to read the value but getting null value while i read code snippet as :-

  1. viper.SetConfigName("config")
  2. viper.AddConfigPath(".")
  3. err := viper.ReadInConfig()
  4. if err != nil {
  5. fmt.Println("Config not found...")
  6. }
  7. }
  8. var apiTrackingID = viper.GetString("API_TRACKING_ID")

Please help to find the issue in my config file or implementation in Go.

答案1

得分: 0

最好检查一下这个链接:https://github.com/spf13/viper,代码片段看起来不错...也许$GOPATH和$GOHOME可能会有问题。

而且你的config.yaml文件不正确,缺少双引号。

API_TOKEN应该像这样:"sdsaxxxdfds123",希望这能解决你的问题。

英文:

better to check this https://github.com/spf13/viper the code snippet looks good ..may be there can be issue with $GOPATH and $GOHOME

and your config.yaml file is not proper double quotes are missing.

  1. API_TOKEN : "sdsaxxxdfds123"

It has to be like this, hope this resolves your problem.

huangapple
  • 本文由 发表于 2016年11月29日 21:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/40866718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定