donglvhe7591
2017-12-21 10:41
浏览 56
已采纳

发出解析Yaml文件的问题

I have the following struct which filled after parsing yaml file The issue is that the quote is missing

File in.yaml

e.g.

_schema: "3.0.0"
bar:
- one

File out.yaml

_schema: 3.0.0
bar:
- one

As you can see I got 3.0.0 instead the “3.0.0” , Any idea how to overcome this

This is a small program which I've created to demonstrate the issue

package main

import (
    "gopkg.in/yaml.v2"
    "io/ioutil"
)

type Config struct {
    Schema string `yaml:"_schema"`
    Bar []string
}

func main() {

    cfg := Config{}
    source, err := ioutil.ReadFile("in.yaml")
    if err != nil {
        panic(err)
    }
    err = yaml.Unmarshal([]byte(source), &cfg)
    if err != nil {
        panic(err)
    }
    y, _ := yaml.Marshal(&cfg)
    err = ioutil.WriteFile("out.yaml", y, 0644)}

}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyue1988 2017-12-21 13:31
    已采纳

    YAML standard doesn't require quoting of all values, only when you want to use escape sequences or you are using certain characters.

    This library you are using adds "..." around the string only when it's necessary. For example if you used a colon in string (try: "3:0:0") it'll stay in double quotes.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题