我将以下配置文件定义为 toml 文件:
[staging]
project-id = "projectId"
cluster-name = "cluster"
zone = "asia-southeast1-a"
然后,我有这个结构
type ConfigureOpts struct {
GCPProjectID string `json:"project-id"`
ClusterName string `json:"cluster-name"`
Zone string `json:"zone"`
}
请注意,我的 ConfigureOpts 字段名格式与配置文件中定义的格式不同。
我试过这段代码,但失败了
test_opts := ConfigureOpts{}
fmt.Printf("viper.staging value %+v\n", viper.GetStringMap("staging"))
viper.UnmarshalKey("staging", &test_opts)
fmt.Printf("testUnmarshall %+v\n", test_opts)
这是输出
viper.staging value map[zone:asia-southeast1-a project-id:projectId cluster-name:cluster]
testUnmarshall {GCPProjectID: ClusterName: Zone:asia-southeast1-a AuthMode: AuthServiceAccount:}