publish select
This commit is contained in:
parent
a5156639ae
commit
d19153e5f6
@ -31,7 +31,7 @@ Channels
|
|||||||
# Channel Buffering
|
# Channel Buffering
|
||||||
# Channel Directions
|
# Channel Directions
|
||||||
# Synchronization
|
# Synchronization
|
||||||
# Select
|
Select
|
||||||
# Timeouts
|
# Timeouts
|
||||||
# Scatter Gather
|
# Scatter Gather
|
||||||
# Rate Limiting
|
# Rate Limiting
|
||||||
|
@ -13,12 +13,13 @@ func f(from string) {
|
|||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Suppose we have a function call `f(s)`. Here's how
|
// Suppose we have a function call `f(s)`. Here's how
|
||||||
// we'd call that in the usual way, running it inline.
|
// we'd call that in the usual way, running it
|
||||||
|
// synchronously.
|
||||||
f("direct")
|
f("direct")
|
||||||
|
|
||||||
// To invoke this function in a goroutine, use
|
// To invoke this function in a goroutine, use
|
||||||
// `go f(s)`. This will start a new, concurrently
|
// `go f(s)`. This new goroutine will execute
|
||||||
// executing goroutine.
|
// concurrently with the current one.
|
||||||
go f("goroutine")
|
go f("goroutine")
|
||||||
|
|
||||||
// You can also start a goroutine for an anonymous
|
// You can also start a goroutine for an anonymous
|
||||||
@ -27,7 +28,7 @@ func main() {
|
|||||||
fmt.Println("going")
|
fmt.Println("going")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Our two goroutines are running asynchrously in
|
// Our two goroutines are running asynchronously in
|
||||||
// separate goroutines now, so execution falls through
|
// separate goroutines now, so execution falls through
|
||||||
// to here. This `Scanln` code requires we press a key
|
// to here. This `Scanln` code requires we press a key
|
||||||
// before the program exits.
|
// before the program exits.
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# When we run this program, we see the output of the
|
# When we run this program, we see the output of the
|
||||||
# direct call first, then the interleaved output of the
|
# blocking call first, then the interleaved output of the
|
||||||
# two gouroutines. This interleaving reflects the
|
# two gouroutines. This interleaving reflects the
|
||||||
# goroutines being run concurrently by the Go runtime.
|
# goroutines being run concurrently by the Go runtime.
|
||||||
$ go run goroutines.go
|
$ go run goroutines.go
|
||||||
|
@ -1,32 +1,52 @@
|
|||||||
|
// Go's _select_ lets you wait on multiple channel
|
||||||
|
// operations. Combining goroutines and channels with
|
||||||
|
// select is powerful feature of Go.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// For our example we'll select accross two channels.
|
||||||
c1 := make(chan string)
|
c1 := make(chan string)
|
||||||
c2 := make(chan string)
|
c2 := make(chan string)
|
||||||
|
|
||||||
|
// This third channel will indicate when we're done
|
||||||
|
// and can exit the program.
|
||||||
d := make(chan bool, 1)
|
d := make(chan bool, 1)
|
||||||
|
|
||||||
|
// The first two channels will receive a value after
|
||||||
|
// some amount of time, to simulate e.g. blocking RPC
|
||||||
|
// operations executing in concurrent goroutines.
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(time.Second * 1)
|
time.Sleep(time.Second * 1)
|
||||||
c1 <- "from 1"
|
c1 <- "one"
|
||||||
}()
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
c2 <- "from 2"
|
c2 <- "two"
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// We'll use `select` to await both of these values
|
||||||
|
// simultaneously, printing each one as it arrives.
|
||||||
|
// Once we've received both, we'll send a value to the
|
||||||
|
// `d` channel indicating that we're ready to proceed.
|
||||||
go func() {
|
go func() {
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
select {
|
select {
|
||||||
case msg1 := <-c1:
|
case msg1 := <-c1:
|
||||||
fmt.Println(msg1)
|
fmt.Println("received", msg1)
|
||||||
case msg2 := <-c2:
|
case msg2 := <-c2:
|
||||||
fmt.Println(msg2)
|
fmt.Println("received", msg2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
d <- true
|
d <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// Since all other code is running in concurrent
|
||||||
|
// goroutines, we'll executing a blocking receive
|
||||||
|
// here to await completion of our example.
|
||||||
<-d
|
<-d
|
||||||
}
|
}
|
||||||
|
10
examples/select/select.sh
Normal file
10
examples/select/select.sh
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# We receive the values `"one"` and then `"two"` as
|
||||||
|
# expected.
|
||||||
|
$ time go run select.go
|
||||||
|
received one
|
||||||
|
received two
|
||||||
|
|
||||||
|
# Note that the total execution time is only ~2 seconds
|
||||||
|
# since both the 1 and 2 second `Sleeps` execute
|
||||||
|
# concurrently.
|
||||||
|
real 0m2.245s
|
Loading…
x
Reference in New Issue
Block a user