我正在编写一个 trie DS,将 json gzip 压缩到一个文件中trieSample.json.gz,然后将其读回结构中。奇怪的是,解组成功但结构未填充。
我试过 json.Unmarshal 和 json.Decoder 都无济于事。需要帮助找到我在这里缺少的东西。读取时不会抛出任何错误,只是该结构没有任何键。如果我尝试正常的 json marshal -> 写入文件并从文件读取 -> Unmarshal,它会正常工作。
var charSet = "0123456789bcdefghjkmnopqrstuvwxyz"
const logTagSlice = "trie.log"
type trieSlice struct {
Children []*tNode `json:"c"`
Charset map[int32]int8 `json:"l"` // Charset is the legend of what charset is used to create the keys and how to position them in trie array
logger loggingapi.Logger `json:"-"`
capacity int `json:"-"` // capacity is the slice capacity to have enough to hold all the characters in the charset
}
type tNode struct {
Children []*tNode `json:"c"` // children represents the next valid runes AFTER the current one
IsLeaf bool `json:"e,omitempty"` // isLeaf represents if this node represents the end of a valid word
Value int16 `json:"v,omitempty"` // value holds the corresponding value for key value pair, where key is the whole tree of nodes starting from parent representing a valid word
}
// NewTrieSlice returns a Trie, charset represents how the children need to be positioned in the array
func NewTrieSlice(charset string, logger loggingapi.Logger) *trieSlice {
m := map[int32]int8{}
for index, r := range charset {
m[r] = int8(index)
}
return &trieSlice{
Charset: m,
Children: make([]*tNode, len(charset)),
logger: logger,
capacity: len(charset),
}
}
func newNode(capacity int) *tNode {
return &tNode{
Children: make([]*tNode, capacity),
}
}
spew.Dump 显示 trieSlice Children 数组包含所有 nil 元素
慕盖茨4494581
相关分类