小编Ino*_*dle的帖子

Go-yaml 控制字符不允许错误

我正在尝试制作一个非常简单的 ssh 地址簿程序。获取有关 ssh 地址的一些信息并将它们存储在 yaml 文档中。我这样做的部分原因是为了学习一些关于 Go 的知识,但遇到了一个小问题。我可以序列化数据并将文档放入文件中,但是当我尝试读回它时出现此错误:
yaml: control characters are not allowed

我不确定此错误消息的含义,谷歌搜索没有产生任何有用的结果。有任何想法吗?

这些是我用来组织数据的结构:

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"`
}   
Run Code Online (Sandbox Code Playgroud)

它将我的数据放入的格式是:

---
entrieslist:
 - name: entry1
   command:
     addr: somewhere
     port: 22
     uname: someone
 - name: entry2 ... etc 
Run Code Online (Sandbox Code Playgroud)

我用 YAML 验证器检查了这个 ^^,它是合法的 YAML。这是我读取文件的函数:

// CONF is the path to the …
Run Code Online (Sandbox Code Playgroud)

yaml go

3
推荐指数
1
解决办法
8214
查看次数

在Go中实现堆栈以便存储结构的正确方法是什么?

我正在尝试制作一个存储一系列霍夫曼树结构的堆栈.目前我正在使用我在github上找到的实现.

package util

type item struct {
    value interface{}
    next  *item
}

//Stack the implementation of stack
//this stack is not thread safe!
type Stack struct {
    top  *item
    size int
}
// Basic stack methods...
Run Code Online (Sandbox Code Playgroud)

问题是当我将我的霍夫曼树结构存储在堆栈中时,我不能使用霍夫曼树的任何字段,例如左/右子.

package huffmantree

type HuffmanTree struct {
    freq   int
    value  byte
    isLeaf bool
    left   *HuffmanTree
    right  *HuffmanTree
    code   []bool
    depth  int
}
Run Code Online (Sandbox Code Playgroud)

我怎么能在Go中实现一个堆栈,它将正确存储结构并允许访问它们的字段?

编辑:我尝试用(huffmantree结构)替换该interface {}部分huffmantree.HuffmanTree并得到此错误消息:

can't load package: import cycle not allowed
package github.com/inondle/huffman/util
    imports github.com/inondle/huffman/huffmantree
    imports github.com/inondle/huffman/util
import cycle …
Run Code Online (Sandbox Code Playgroud)

stack types go

2
推荐指数
1
解决办法
701
查看次数

标签 统计

go ×2

stack ×1

types ×1

yaml ×1