在另一个例程中调用 SetReadDeadline 是否正确?

Is it a right usage calling SetReadDeadline in another routine?

在大多数示例中,SetReadDeadline 在调用 net.Conn.Read() 之前被调用。

在我的程序中,为了打断我的 net.Conn.Read(),我在另一个 thread/routine 中调用了 net.Conn.SetReadDeadline(time.Now())。方法对吗?

谢谢。

如文档所述:

SetReadDeadline sets the deadline for future Read calls and any currently-blocked Read call. A zero value for t means Read will not time out.

您可以为阻塞的读取调用调用 SetReadDeadline 的唯一方法是从另一个 goroutine,所以这没问题。