123456789101112131415161718192021222324252627282930313233343536 |
- package main
- import (
- "context"
- "fmt"
- "time"
- )
- var c chan bool
- func init() {
- c = make(chan bool, 10)
- for i := 0; i < 10; i++ {
- c <- true
- }
- }
- func main() {
- ctx, cancel := context.WithCancel(context.Background())
- close(c)
- println(1)
- cancel()
- println(2)
- exit:
- for {
- select {
- case data, ok := <-c:
- fmt.Println(data, ok)
- time.Sleep(1 * time.Second)
- case <-ctx.Done():
- println(3)
- break exit
- }
- }
- println(4)
- }
|