将YAML字符串数组转换为Golang结构体字段。

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

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)

huangapple
  • 本文由 发表于 2021年6月6日 23:17:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/67860666.html
匿名

发表评论

匿名网友

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

确定