我正在浏览 "A Tour of Go"教程。
我想检查这个问题的答案:
Note: a call to
fmt.Sprint(e)
inside theError
method will send the program into an infinite loop. You can avoid this by convertinge
first:fmt.Sprint(float64(e))
. Why?
我相信这是因为当 Sprint
函数被调用时,由于错误是非零的, Error function()
将再次被调用,等等,导致一个无限循环。
最佳答案
fmt.Sprint(e)
将调用 e.Error()
将值 e
转换为 string
。如果 Error()
方法调用 fmt.Sprint(e)
,则程序会递归直到内存不足。
您可以通过将 e
转换为没有 String
或 Error
方法的值来打破递归。
关于go - 为什么在 Error() 方法内调用 fmt.Sprint(e) 会导致无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54935629/