ZhangZhihui's Blog  

 

 

func main() {
    messageCh := make(chan int, 10)
    disconnectCh := make(chan struct{}, 1)

    for i := 0; i < 10; i++ {
        messageCh <- i
    }

    go func() {
        for {
            select {
            case v := <-messageCh:
                fmt.Println(v)
            case <-disconnectCh:
                fmt.Println("disconnection, return")
                return
            }
        }
    }()

    time.Sleep(10 * time.Microsecond)
    disconnectCh <- struct{}{}

    time.Sleep(time.Second)
}

 

zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
0
1
2
3
4
5
6
7
8
9
disconnection, return
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
0
1
2
3
disconnection, return
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
0
disconnection, return

 

 

 

posted on 2024-02-23 16:09  ZhangZhihuiAAA  阅读(3)  评论(0编辑  收藏  举报