dqb78642 2015-11-15 08:30
浏览 1331
已采纳

转到Yaml控制字符不允许错误

I'm trying to make a really simple ssh address book program. Takes some info about ssh addresses and stores them in a yaml document. I'm doing it partly to learn a bit about Go and am having a small problem. I can serialize the data and put a document into a file but I'm getting this error when I try to read it back:
yaml: control characters are not allowed

I'm not sure what this error message means, googling didn't yield any helpful results. Any ideas?

These are the structs I'm using to organize the data:

type EntriesList struct {
    SSHEntries []SSHEntry `yaml:"sshentries"`
}   

type SSHEntry struct {
    Name    string `yaml:"name"`
    Command SSHCmd `yaml:"command"`
}   

type SSHCmd struct {
    Addr  string `yaml:"addr"`
    Port  int    `yaml:"port"`
    Uname string `yaml:"uname"`
}   

The format it puts my data into is:

---
entrieslist:
 - name: entry1
   command:
     addr: somewhere
     port: 22
     uname: someone
 - name: entry2 ... etc 

I checked this ^^ with a YAML validator and it is legal YAML. Here is my function to read the file:

// CONF is the path to the file
func readConf(CONF string) *EntriesList {
    configFile := openConfigFile(CONF)
    defer configFile.Close()

    buffer := make([]byte, 512, 512)
    _, err := configFile.Read(buffer)
    check(err)

    var entries EntriesList
    err = yaml.Unmarshal(buffer, &entries)
    data, _ := yaml.Marshal(entries)
    fmt.Println(string(data))

    return &entries
}
  • 写回答

2条回答 默认 最新

  • drema2014 2015-11-15 23:59
    关注

    Figured it out, the problem was that my buffer was too big. If you have a []byte that is too big then go-yaml will read those extra bytes as characters and throw errors. I changed my code to :

    func readConf(CONF string) *EntriesList {
        confiFile, err := ioutil.ReadFile(CONF)
        check(err)
        var entries EntriesList
        err = yaml.Unmarshal(confiFile, &entries)
        check(err)
        return &entries
    }
    

    And it worked as expected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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