31 lines
438 B
Go
31 lines
438 B
Go
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)
|
|
}
|
|
}
|
|
|
|
}
|