为了账号安全,请及时绑定邮箱和手机立即绑定

运行时:goroutine 堆栈超过 1000000000 字节限制,当将结构保存到 json 时

运行时:goroutine 堆栈超过 1000000000 字节限制,当将结构保存到 json 时

Go
喵喵时光机 2023-06-05 17:20:17
我已经定义了一个 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条经验 获得超3个赞

问题是每个人Node都引用了它的父母,以及它的孩子。因此,当它对一个孩子进行编码时,对于父字段,它会再次对父字段进行编码,对于该父字段,它会再次对子字段进行编码,等等。一个简单的解决方案是在编码时不使用该Parent字段

Parent   *Node `json:"-"`

这将阻止循环。

https://play.golang.org/p/BdVgMNjlZOa


查看完整回答
反对 回复 2023-06-05
  • 1 回答
  • 0 关注
  • 151 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信