英文:
Passing a string map as an environment variable in Go using Viper
问题
我正在为您翻译以下内容:
对于我正在进行的一个项目,我尝试使用Viper将字符串映射作为环境变量传递。我尝试了几种方法,但都没有成功。当我从代码中读取环境变量时,它是空的。这是我使用的代码:
// 配置Viper
viper.SetEnvPrefix("CONFIG")
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
// 读取配置值,我尝试了以下所有变体:
fmt.Print(viper.GetString("options.values"))
fmt.Print(viper.GetStringMapString("options.values"))
fmt.Print(viper.GetStringMap("options.values"))
这是我如何传递值的方式:
CONFIG_OPTIONS_VALUES_ROOT="."
我还尝试过:
CONFIG_OPTIONS_VALUES="{"root": ".", "cmd": "exec", "logging": "on"}"
我想处理传递给环境变量的值的方式是:
values := viper.GetStringMapString("options.values")
for key, val := range values {
fmt.Printf("Key: %s, Value: %s", key, val)
}
如果我将此配置写入配置文件并使用Viper读取它,我可以完美地执行此操作:
options:
values:
root: .
cmd: exec
logging: on
#更多值可以在这里添加
希望有人能指导我正确的方向。
英文:
For a project I'm working on I'm trying to pass a map of stings as an environment variable using Viper. I tried several approaches to achieve this but with no success. When I read the env variable from code it is empty. This is the code I'm using:
// To configure viper
viper.SetEnvPrefix("CONFIG")
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
// To read the configuration value I tried all this variants:
fmt.Print(viper.GetString("options.values"))
fmt.Print(viper.GetStringMapString("options.values"))
fmt.Print(viper.GetStringMap("options.values"))
And this is how I'm passing the value:
CONFIG_OPTIONS_VALUES_ROOT="."
I've also tried:
CONFIG_OPTIONS_VALUES="{"root": ".","cmd": "exec", "logging": "on"}"
The way I want to process the value passes in the env variable is:
values := viper.GetStringMapString("options.values")
for key, val := range values {
fmt.Printf("Key: %s, Value: %s", key, val)
}
Which I can perfectly do if I write this configuration in a config file and I read it using viper:
options:
values:
root: .
cmd: exec
logging: on
#more values can be added here
Hope someone can point me in the right direction here.
答案1
得分: 3
我已经进行了一些调查,似乎你没有正确设置环境变量的值,以及你如何使用viper调用它。以下是一个示例,并请随意评论你的任何想法:
package main
import (
"bytes"
"fmt"
"github.com/spf13/viper"
"strings"
)
func main() {
//将配置类型设置为JSON
viper.SetConfigType("json")
//将环境前缀设置为CONFIG
viper.SetEnvPrefix("CONFIG")
viper.AutomaticEnv()
//将_替换为.
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
//获取在CONFIG_OPTIONS_VALUES环境变量中设置的字符串
var jsonExample = []byte(viper.GetString("options.values"))
viper.ReadConfig(bytes.NewBuffer(jsonExample))
//将options字段的子JSON字符串转换为map[string]string
fmt.Println(viper.GetStringMapString("options"))
}
调用方式如下:
CONFIG_OPTIONS_VALUES="{\"options\": {\"root\": \".\", \"cmd\": \"exec\", \"logging\": \"on\"}}" go run main.go
英文:
I have been investigating a bit and it seems that you are not set the value of your environment variable properly, as well as how you are calling it with viper. Find below an example of that and feel free to comment any thought that you have:
package main
import (
"bytes"
"fmt"
"github.com/spf13/viper"
"strings"
)
func main() {
//Configure the type of the configuration as JSON
viper.SetConfigType("json")
//Set the environment prefix as CONFIG
viper.SetEnvPrefix("CONFIG")
viper.AutomaticEnv()
//Substitute the _ to .
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
//Get the string that is set in the CONFIG_OPTIONS_VALUES environment variable
var jsonExample = []byte(viper.GetString("options.values"))
viper.ReadConfig(bytes.NewBuffer(jsonExample))
//Convert the sub-json string of the options field in map[string]string
fmt.Println(viper.GetStringMapString("options"))
}
And how it would be call:
CONFIG_OPTIONS_VALUES="{\"options\": {\"root\": \".\", \"cmd\": \"exec\", \"logging\": \"on\"}}" go run main.go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论