错误,无限循环 [英] Error, infinite loop
问题描述
我想查看这个问题的答案:
注意:在 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屋!