douzhaobo6488
2018-06-18 15:06Struct Json封送处理导致堆栈溢出
Problem
When I use the NewChild() function in my code and than afterwards marshal the "Report" Struct to JSON I get a Stack Overflow (goroutine stack exceeds 1000000000-byte limit)
After researching I found out that it should have to do something with infinite recursion, but I don't have a clue why my code should have that.
Code
type Report struct{
TestSuites []ReportElement
Tests int
Success int
Failed int
Root *ReportElement
CurrentElement *ReportElement `json:"-"`
}
type ReportElement struct{
Success bool
Time bool
LogStorage []string
Childs []ReportElement
Parent *ReportElement
}
func (r *Report) NewChild(){
newElem := ReportElement{}
newElem.Parent = r.CurrentElement
newElem.Childs = make([]ReportElement,0)
newChilds := append(r.CurrentElement.Childs,newElem)
r.CurrentElement.Childs = newChilds
r.CurrentElement = &newElem
}
func TestReporterStackOverflow(t *testing.T) {
report := NewReport()
report.NewChild()
jsonReport,err := json.MarshalIndent(report,""," ")
if err != nil{
t.Fatal(err)
}
t.Log(jsonReport)
}
Ideas
Actually I am not sure if it has to do something with my code as the stacktrace leads to "/usr/local/go/src/encoding/json/encode.go".
Thanks a lot for your help!
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- Struct Json封送处理导致堆栈溢出
- it技术
- 互联网问答
- IT行业问题
- 计算机技术
- 编程语言问答
- 1个回答
- UnmarshalJSON导致堆栈溢出
- it技术
- 互联网问答
- IT行业问题
- 计算机技术
- 编程语言问答
- 1个回答
- 如何在UnmarshalJSON中调用json.Unmarshal而不引起堆栈溢出?
- struct
- 1个回答