Created
March 19, 2022 08:23
-
-
Save nikolaydubina/49307b8939b619de7191e2121d775432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://go.dev/play/p/RKU8IstAlhf | |
// Does close() signals select? Yes | |
// Is channel picked randomly? Yes | |
// If there is long buffered channel with many entries will cancel() / other channel be triggered? Yes | |
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
long := make(chan int, 100) | |
for i := 0; i < 100; i++ { | |
long <- i | |
} | |
stop := make(chan bool) | |
go func() { | |
t := time.NewTicker(time.Millisecond) | |
for { | |
select { | |
case <-t.C: | |
fmt.Println("tick") | |
case <-stop: | |
fmt.Println("stop") | |
return | |
case i := <-long: | |
fmt.Printf("long %d\n", i) | |
time.Sleep(time.Millisecond) | |
} | |
} | |
}() | |
fmt.Println("main: start") | |
time.Sleep(time.Millisecond * 300) | |
fmt.Println("main: close") | |
close(stop) | |
time.Sleep(time.Second) | |
fmt.Println("main: exit") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment