gobyexample/cselect/cselect.go

31 lines
438 B
Go
Raw Permalink Normal View History

2024-04-28 10:03:42 +00:00
package cselect
import (
"fmt"
"time"
)
func Select() {
c1, c2 := make(chan string), make(chan string)
go func() {
time.Sleep(2 * time.Second)
c1 <- "First secret!"
}()
go func() {
time.Sleep(2 * time.Second)
c2 <- "Second secret!"
}()
for range 2 {
select {
case msg1 := <-c1:
fmt.Println("Received something from c1:", msg1)
case msg2 := <-c2:
fmt.Println("Received something from c2:", msg2)
}
}
}