mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-21 17:07:18 +08:00
50 lines
794 B
Go
50 lines
794 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
messageCh := make(chan int, 10)
|
|
disconnectCh := make(chan struct{})
|
|
|
|
go listing1(messageCh, disconnectCh)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
messageCh <- i
|
|
}
|
|
disconnectCh <- struct{}{}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
|
|
func listing1(messageCh <-chan int, disconnectCh chan struct{}) {
|
|
for {
|
|
select {
|
|
case v := <-messageCh:
|
|
fmt.Println(v)
|
|
case <-disconnectCh:
|
|
fmt.Println("disconnection, return")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func listing2(messageCh <-chan int, disconnectCh chan struct{}) {
|
|
for {
|
|
select {
|
|
case v := <-messageCh:
|
|
fmt.Println(v)
|
|
case <-disconnectCh:
|
|
for {
|
|
select {
|
|
case v := <-messageCh:
|
|
fmt.Println(v)
|
|
default:
|
|
fmt.Println("disconnection, return")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|