mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
update book code
This commit is contained in:
23
eBook/examples/chapter_14/chaining.go
Normal file
23
eBook/examples/chapter_14/chaining.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var ngoroutine = flag.Int("n", 100000, "how many goroutines")
|
||||
|
||||
func f(left, right chan int) { left <- 1+<-right }
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
leftmost := make(chan int)
|
||||
var left, right chan int = nil, leftmost
|
||||
for i := 0; i < *ngoroutine; i++ {
|
||||
left, right = right, make(chan int)
|
||||
go f(left, right)
|
||||
}
|
||||
right <- 0 // bang!
|
||||
x := <-leftmost // wait for completion
|
||||
fmt.Println(x) // 100000, ongeveer 1,5 s
|
||||
}
|
Reference in New Issue
Block a user