Source file
test/chan/goroutines.go
1
2
3
4
5
6
7
8
9
10 package main
11
12 import (
13 "os"
14 "strconv"
15 )
16
17 func f(left, right chan int) {
18 left <- <-right
19 }
20
21 func main() {
22 var n = 10000
23 if len(os.Args) > 1 {
24 var err error
25 n, err = strconv.Atoi(os.Args[1])
26 if err != nil {
27 print("bad arg\n")
28 os.Exit(1)
29 }
30 }
31 leftmost := make(chan int)
32 right := leftmost
33 left := leftmost
34 for i := 0; i < n; i++ {
35 right = make(chan int)
36 go f(left, right)
37 left = right
38 }
39 go func(c chan int) { c <- 1 }(right)
40 <-leftmost
41 }
42
View as plain text