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

我已经定义了一个 Trie 数据结构的 go struct。


type Node struct {

Val      rune

IsWord   bool

IsRoot   bool

Parent   *Node

Children map[rune]*Node

}

type Trie struct {

Root *Node

}

trie := algorithms.InitTrie()

但是,它会引发错误


runtime: goroutine stack exceeds 1000000000-byte limit

fatal error: stack overflow

runtime stack:

runtime.throw(0x10e9426, 0xe)

/usr/local/go/src/runtime/panic.go:605 +0x95

runtime.newstack(0x0)

/usr/local/go/src/runtime/stack.go:1050 +0x6e1

runtime.morestack()

/usr/local/go/src/runtime/asm_amd64.s:415 +0x86

当我插入一些单词并将其保存到 json 文件中时。


fmt.Println(json.Marshal(&trie))


喵喵时光机
浏览 251回答 1
1回答

Helenr

问题是每个人Node都引用了它的父母,以及它的孩子。因此,当它对一个孩子进行编码时,对于父字段,它会再次对父字段进行编码,对于该父字段,它会再次对子字段进行编码,等等。一个简单的解决方案是在编码时不使用该Parent字段Parent   *Node `json:"-"`这将阻止循环。https://play.golang.org/p/BdVgMNjlZOa
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go