您的位置:首页 > Web前端 > JavaScript

golang json.Marshal使用中遇到的崩溃问题

2017-01-22 12:43 656 查看
1000000000-byte limit fatal error: stack overflow

出现上述类似崩溃报错

原因是待Marshal的结构体中包含了一个指向上一层的指针,在被Marshal的时候循环使用,导致stack overflow

解决方案,不需要Marshal的数据,在后面加上 json:”-“

原代码类似于下:

type datas struct{
a int
b int
da data
}

type data struct{
c int
ds *datas
}

func main(){
var a = new(datas)
a.da.ds=a
json.Marshal(a)
}


修改为

type datas struct{
a int
b int
da data
}

type data struct{
c int
ds *datas `json:"-"`
}

func main(){
var a = new(datas)
a.da.ds=a
json.Marshal(a)
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  golang 解决方案