英文:
Tell whether net.Listener is dead
问题
在Go语言中,调用net.Listener
类型的Accept
方法会返回一个错误。然而,有没有办法区分临时错误(例如,连接设置失败)和永久错误(例如,监听器已经关闭,比如强制删除的Unix域套接字文件)?如果我无法区分这两种错误,就有可能陷入无限循环,并且每次调用Accept
都会立即返回一个错误,导致错误不断输出。
英文:
In Go, a call to the net.Listener
type's Accept
method returns an error. However, is there a way to tell the difference between a transient error (ie, this connection failed to set up) vs a permanent error (ie, the listener is dead, such as a Unix domain socket file that was forcibly removed)? If I can't tell the difference, I run the risk of infinite looping and spitting out errors as fast as I can since each Accept
call will immediately return an error.
答案1
得分: 2
我明白了。net
包返回的错误可能是 net.Error
类型,该类型定义了 Temporary() bool
方法,用于判断错误是否是临时的。
英文:
Figured it out. Errors returned by the net
package may be of the net.Error
type, which defines the Temporary() bool
method which reports whether the error is temporary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论