【问题标题】:Empty output while trying to convert a yaml data into a struct [duplicate]尝试将yaml数据转换为结构时为空输出[重复]
【发布时间】:2018-12-01 11:01:47
【问题描述】:

我正在尝试将 yaml 数据转换为结构并打印它。我为这个程序得到的输出是空的。

package main

import (
"fmt"

"gopkg.in/yaml.v2"
)

type example struct {
    variable1 string
    variable2 string
}

func main() {
    var a example
    yaml.Unmarshal([]byte("variable1: asd\nvariable2: sdcs"), &a)
    fmt.Println(a.variable1)
}

【问题讨论】:

    标签: go struct yaml


    【解决方案1】:

    Unmarshaldocumentation 声明

    结构字段仅在它们被导出(具有大写首字母)并且使用小写的字段名称作为默认键进行解组时才被解组。

    因此,将结构元素大写是正确的做法。

    type example struct {
        Variable1 string
        Variable2 string
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 2015-05-30
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      相关资源
      最近更新 更多