概述

利用 channel (通道)time.After() 办法实现超时管制。

例子

package mainimport (    "fmt"    "time")func main() {    ch := make(chan bool)    go func() {        defer func() {            ch <- true        }()        time.Sleep(2 * time.Second) // 模仿超时操作    }()    select {    case <-ch:        fmt.Println("ok")    case <-time.After(time.Second):        fmt.Println("timeout!")    }}// $ go run main.go// 输入如下/**  timeout!*/

分割我