系统库已经写好了判断超时的接口了,直接按下面调用就行额
```go
package main
import (
"log"
"net"
"net/http"
"net/url"
"time"
)
func main() {
c, err := net.Dial("udp", "127.0.0.1:12345")
if err != nil {
log.Fatal(err)
}
c.SetDeadline(time.Now().Add(time.Second))
var buf = make([]byte, 8)
c.Write(buf)
_, err = c.Read(buf)
if err != nil {
netErr, ok := err.(*net.OpError)
if ok && netErr.Timeout() {
log.Println("net timeout")
}
}
req, err := http.NewRequest(http.MethodGet, "http://asdasd", nil)
if err != nil {
log.Fatal(err)
}
client := http.Client{Timeout: time.Second}
_, err = client.Do(req)
if err != nil {
urlErr, ok := err.(*url.Error)
if ok && urlErr.Timeout() {
log.Println("http timeout")
}
}
}
```
有疑问加站长微信联系(非本文作者)