一种Go使用tcp检测超时的方式

c.SetReadDeadline(time.Now())
if _, err := c.Read(one); err == io.EOF {
  l.Printf(logger.LevelDebug, "%s detected closed LAN connection", id)
  c.Close()
  c = nil
} else {
  var zero time.Time
  c.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
}
For detecting a timeout, it suggests:

if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
  //......
}

Source Page: http://stackoverflow.com/a/12741495

 
原文地址:https://www.cnblogs.com/Jim-william/p/5625649.html