-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
使用了这个库几个月,想问下能不能支持使用环境变量方式 #3
Comments
Merged
func NewWithViper(viper *viper.Viper) (*Goss, error) {} 这种方式可否满足你的需要? |
可以,作为一个Golang新手,我有一个疑问,如果其他程序是把配置文件解析到了结构体中,类似这样
您提供的这种方式是不是就不能使用了? |
Merged
办法是有的,就是不太优雅,你可以参考一下下面的这种写法: type Minio struct {
Endpoint string `yaml:"endpoint"`
Bucket string `yaml:"bucket"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
UseSsl bool `yaml:"use_ssl"`
}
func newViper(name string, value interface{}) *viper.Viper {
v := viper.New()
r := reflect.ValueOf(value)
for i := 0; i < r.NumField(); i++ {
v.Set(name+"."+r.Type().Field(i).Tag.Get("yaml"), r.Field(i).Interface())
}
return v
}
func TestMinio(t *testing.T) {
m := Minio{
Endpoint: "aa",
Bucket: "bb",
AccessKey: "cc",
SecretKey: "dd",
UseSsl: true,
}
v := newViper("minio", m)
assert.Equal(t, "aa", v.GetString("minio.endpoint"))
assert.Equal(t, "bb", v.GetString("minio.bucket"))
assert.Equal(t, "cc", v.GetString("minio.access_key"))
assert.Equal(t, "dd", v.GetString("minio.secret_key"))
assert.Equal(t, true, v.GetBool("minio.use_ssl"))
} 可以根据配置的结构体和 oss 类型来创建一个
|
明白了,感谢解答。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用了这个库几个月,想问下能不能支持使用环境变量方式,因为我把这个库作为第三方库使用的时候发现需要重复读取几次配置文件。我的程序本来就使用了viper读取配置文件,默认配置文件里面已经包含了您这个库的配置参数,现在把您的库作为一个依赖库的话,就必须再读取一次配置文件才行。能不能增加一个函数,直接使用环境变量参数,这样就可以不用重复读取几次配置文件了。
The text was updated successfully, but these errors were encountered: