错误,无限循环 [英] Error, infinite loop

查看:236
本文介绍了错误,无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想查看这个问题的答案:



注意:在 Error 方法内调用 fmt.Sprint(e)会将程序发送到无限循环。你可以通过首先转换 e 来避免这种情况: fmt.Sprint(float64(e))。为什么?



我相信这是因为当调用 Sprint 函数时,由于错误不是零, Error函数()将再次被调用,等等,导致无限循环。 c> e.Error()将值 e 转换为字符串。如果 Error()方法调用 fmt.Sprint(e),那么程序会递归直到内存不足。 / p>

您可以通过将 e 转换为一个没有 String Error 方法。


I am going through the Go language tutorial.

I would like to check the answer to this question:

Note: a call to fmt.Sprint(e) inside the Error method will send the program into an infinite loop. You can avoid this by converting e first: fmt.Sprint(float64(e)). Why?

I believe this is because when the Sprint function is called, since the error is non-nil, the Error function() will again be called, and so forth, resulting in an infinite loop.

解决方案

fmt.Sprint(e) will call e.Error() to convert the value e to a string. If the Error() method calls fmt.Sprint(e), then the program recurses until out of memory.

You can break the recursion by converting the e to a value without a String or Error method.

这篇关于错误,无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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