运行时:goroutine 堆栈超过 1000000000 字节限制,致命错误:打印嵌套结构时堆栈溢出 [英] runtime: goroutine stack exceeds 1000000000-byte limit, fatal error: stack overflow on printing a nested struct

查看:31
本文介绍了运行时:goroutine 堆栈超过 1000000000 字节限制,致命错误:打印嵌套结构时堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套结构.

type ConfigOne struct {
    // Daemon section from config file.
    Daemon daemon
}
type daemon struct {
    Loglevel int
    Logfile string
}

我在该类型上有一个 String() string 方法,我试图将嵌套的结构元素返回为

And I have a String() string method on that type, which I am trying to return the nested struct elements as

func (c ConfigOne)String()  string{
    return fmt.Sprintf("%+v
", c)
}

当我尝试将其打印为

c := &modules.ConfigOne{}
c.Daemon.Loglevel = 1
c.Daemon.Logfile = "/tmp/test.log"
modules.Logger.Infoln(c.String())

我收到了错误

运行时:goroutine 堆栈超过 1000000000 字节的限制致命错误:堆栈溢出

runtime: goroutine stack exceeds 1000000000-byte limit fatal error: stack overflow

运行时堆栈:runtime.throw(0x6ea3b7, 0xe)...

runtime stack: runtime.throw(0x6ea3b7, 0xe) ...

通过错误后,我可以看到类似于下面的重复行

After going through the error, I could see repeated lines similar to the below one

modules/structs.go:31 +0x77 fp=0xc440100398 sp=0xc440100328go-consume/modules.(*ConfigOne).String(0xc42abcb4e0, 0x70bc08, 0xc42abd6300):1 +0x64 fp=0xc4401003d8 sp=0xc440100398fmt.(*pp).handleMethods(0xc42abd6300, 0xc400000076, 0x410301)

modules/structs.go:31 +0x77 fp=0xc440100398 sp=0xc440100328 go-consume/modules.(*ConfigOne).String(0xc42abcb4e0, 0x70bc08, 0xc42abd6300) :1 +0x64 fp=0xc4401003d8 sp=0xc440100398 fmt.(*pp).handleMethods(0xc42abd6300, 0xc400000076, 0x410301)

最后,在死之前,

modules/structs.go:31 +0xc0 fp=0xc440103d18 sp=0xc440103ca8...省略了其他帧...

modules/structs.go:31 +0xc0 fp=0xc440103d18 sp=0xc440103ca8 ...additional frames elided...

goroutine 17 [系统调用,锁定到线程]:runtime.goexit()

goroutine 17 [syscall, locked to thread]: runtime.goexit()

我相信这是由于进入一些无限递归造成的.

which I believe is caused by going into some infinite recursion.

我碰巧找到了原因并到达了这里,我相信是同一个问题.但是我无法理解该线程中的解释.

I tried my luck to find the cause and reached here, which I believe is the same issue. However I couldn't understand the explanation in that thread.

如果我尝试将单个嵌套结构打印为

If I try to print the individual nested struct as

func (c ConfigOne)String() string{
    //return fmt.Sprintf("%+v
", c.Daemon.Loglevel)
    return fmt.Sprintf("%+v
", c.Daemon)
}

工作正常,日志显示字段为

it is working fine, and log shows the fields as

2017/03/05 01:28:25 go-consume.go:38: INFO: {Loglevel:1 Logfile:/tmp/test.log}

谁能解释一下以前的 String() 方法是如何导致无限递归和 stackoverflow 的,最好的解决方法是什么?

Can someone kindly explain how the former String() method is resulting in an infinite recursion and a stackoverflow, and what is the best way to overcome this?

推荐答案

%v%+v格式使用String() 如果类型实现它.因此,在 String() 函数中对该类型使用 %+v 会导致无限递归.除了在 String() 函数中使用 %+v 之外,您还必须构建自己的字符串,以您认为合适的任何方式显示结构的内容.

The %v and %+v formats use the value of String() if the type implements it. Therefore, using %+v on a type within the String() function for that type causes infinite recursion. Instead of using %+v in the String() function, you'll have to construct your own string, showing the contents of the structure in whatever way you see fit.

这篇关于运行时:goroutine 堆栈超过 1000000000 字节限制,致命错误:打印嵌套结构时堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆