我已經定義了一個 Trie 數據結構的 go struct。type Node struct {Val runeIsWord boolIsRoot boolParent *NodeChildren map[rune]*Node}type Trie struct {Root *Node}trie := algorithms.InitTrie()但是,它會引發錯誤runtime: goroutine stack exceeds 1000000000-byte limitfatal error: stack overflowruntime stack:runtime.throw(0x10e9426, 0xe)/usr/local/go/src/runtime/panic.go:605 +0x95runtime.newstack(0x0)/usr/local/go/src/runtime/stack.go:1050 +0x6e1runtime.morestack()/usr/local/go/src/runtime/asm_amd64.s:415 +0x86當我插入一些單詞并將其保存到 json 文件中時。fmt.Println(json.Marshal(&trie))
1 回答

Helenr
TA貢獻1780條經驗 獲得超4個贊
問題是每個人Node
都引用了它的父母,以及它的孩子。因此,當它對一個孩子進行編碼時,對于父字段,它會再次對父字段進行編碼,對于該父字段,它會再次對子字段進行編碼,等等。一個簡單的解決方案是在編碼時不使用該Parent
字段
Parent *Node `json:"-"`
這將阻止循環。
https://play.golang.org/p/BdVgMNjlZOa
- 1 回答
- 0 關注
- 316 瀏覽
添加回答
舉報
0/150
提交
取消