dongyin6576 2018-05-15 08:58
浏览 49
已采纳

错误地解组Yaml?

I am using yaml to decode yaml file. However, the result is not as expected. The EncryptKey is not extracted. This is my test code:

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v2"
)

var data = `
port: 8080
encryptKey: "jfgjfgkfgd"
`

type Config struct {
    Port       int    `json:"port"`
    EncryptKey string `json:"encryptKey"`
}

func main() {
    t := Config{}

    err := yaml.Unmarshal([]byte(data), &t)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- t:
%v

", t)
}

I got the result like this:--- t: {8080 }


It's my carelessness. I should use the tag yaml instead of json.

  • 写回答

1条回答 默认 最新

  • duanhai7274 2018-05-15 09:16
    关注

    You are using json tags instead of yaml tags. Fix your struct definition like this:

    type Config struct {
        Port       int    `yaml:"port"`
        EncryptKey string `yaml:"encryptKey"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!