英文:
Yaml array of string to golang struct field
问题
我正在尝试使用这个包将简单的YAML配置文件映射到结构体,但没有成功。
config.yaml:
Drivers:
- "/Volumes/V1"
- "/Volumes/V2"
go代码:
type Iconfig struct {
Drivers []string `yaml:"Drivers,flow"`
}
iconfig := Iconfig{}
uerr := yaml.UnmarshalStrict(config_yaml, iconfig)
uerr:
panic: reflect: reflect.Value.Set using unaddressable value [recovered]
panic: reflect: reflect.Value.Set using unaddressable value
fmt.Println(string(config_yaml)):
Drivers:
- "/Volumes/V1"
- "/Volumes/V2"
为什么"/Volumes/V1"
被认为是不可寻址的值?
英文:
I'm trying to map dead simple yaml config file to struct with this package with no success.
config.yaml
Drivers:
- "/Volumes/V1"
- "/Volumes/V2"
go
type Iconfig struct {
Drivers []string `yaml:"Drivers,flow"`
}
iconfig := Iconfig{}
uerr := yaml.UnmarshalStrict(config_yaml, iconfig)
uerr
:
panic: reflect: reflect.Value.Set using unaddressable value [recovered]
panic: reflect: reflect.Value.Set using unaddressable value
fmt.Println(string(config_yaml))
:
Drivers:
- "/Volumes/V1"
- "/Volumes/V2"
Why "/Volumes/V1"
is considered as unaddressable value?
答案1
得分: 1
尝试将您的代码行更改为以下内容:
uerr := yaml.UnmarshalStrict(config_yaml, &iconfig)
英文:
try to change your line to this:
uerr := yaml.UnmarshalStrict(config_yaml, &iconfig)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论