Skip to content

Instantly share code, notes, and snippets.

@nikolaydubina
Created March 19, 2022 08:23
Show Gist options
  • Save nikolaydubina/49307b8939b619de7191e2121d775432 to your computer and use it in GitHub Desktop.
Save nikolaydubina/49307b8939b619de7191e2121d775432 to your computer and use it in GitHub Desktop.
// 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