【问题标题】:Yaml array of string to golang struct fieldYaml 字符串数组到 golang 结构字段
【发布时间】:2021-06-06 15:17:40
【问题描述】:

我正在尝试将死的简单 yaml 配置文件映射到带有 this 包的结构,但没有成功。

config.yaml

Drivers: 
  - "/Volumes/V1"
  - "/Volumes/V2"

去

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" 被认为是不可寻址的值?

【问题讨论】:

    标签: go yaml


    【解决方案1】:

    尝试将您的线路更改为:

    uerr := yaml.UnmarshalStrict(config_yaml, &iconfig)
    

    【讨论】:

    • 天哪,杀了我。先生,感谢您的宝贵时间。
    猜你喜欢
    • 2022-12-19
    • 2021-02-11
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多