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

发出解析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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?