package mainimport ( "fmt" "time")func main() { start := time.Now() c := make(chan interface{}) go func() { time.Sleep(5 * time.Second) close(c) }() fmt.Println("Blocking on read...") select { case a, b := <-c: fmt.Println(a, b) fmt.Printf("Unblocked %v later. \n", time.Since(start)) }}输出:Blocking on read...<nil> falseUnblocked 5.0137958s later.



package mainimport ( "fmt" "time")func main() { done := make(chan interface{}) go func() { time.Sleep(5 * time.Second) close(done) }() workCounter := 0loop: for { select { case <-done: break loop default: fmt.Println("xxx") } workCounter++ time.Sleep(1 * time.Second) } fmt.Printf("Achieved %v cycles of work before signalled to stop.\n", workCounter)}输出:xxxxxxxxxxxxxxxAchieved 5 cycles of work before signalled to stop.