initial commit
This commit is contained in:
commit
493ce92e02
62 changed files with 1213 additions and 0 deletions
39
select/select.go
Normal file
39
select/select.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
//Go's select lets you wait on multiple channel
|
||||
//operations combining goroutines and channels with select
|
||||
//is a powerful feature of go
|
||||
func main(){
|
||||
//For our example well select across two channels.
|
||||
c1 := make(chan string)
|
||||
c2 := make(chan string)
|
||||
|
||||
|
||||
//each channel will receive a value after some
|
||||
//amount of time, to simulate e.g. blocking RPC
|
||||
//operations executing in concurrent goroutines
|
||||
go func(){
|
||||
time.Sleep(1*time.Second)
|
||||
c1 <- "one"
|
||||
}()
|
||||
go func(){
|
||||
time.Sleep(2*time.Second)
|
||||
c2 <- "two"
|
||||
}()
|
||||
|
||||
|
||||
//we'll use select to await both of these values
|
||||
//simultaneously, printing each one as it arrives.
|
||||
for i := 0; i < 2; i++ {
|
||||
select{
|
||||
case msg1 := <-c1:
|
||||
fmt.Println("received", msg1)
|
||||
case msg2 := <-c2:
|
||||
fmt.Println("received", msg2)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue