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

错误地解组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条回答 默认 最新

相关推荐 更多相似问题