Source file
test/chan/fifo.go
1
2
3
4
5
6
7
8
9 package main
10
11 import "os"
12
13 const N = 10
14
15 func AsynchFifo() {
16 ch := make(chan int, N)
17 for i := 0; i < N; i++ {
18 ch <- i
19 }
20 for i := 0; i < N; i++ {
21 if <-ch != i {
22 print("bad receive\n")
23 os.Exit(1)
24 }
25 }
26 }
27
28 func Chain(ch <-chan int, val int, in <-chan int, out chan<- int) {
29 <-in
30 if <-ch != val {
31 panic(val)
32 }
33 out <- 1
34 }
35
36
37 func SynchFifo() {
38 ch := make(chan int)
39 in := make(chan int)
40 start := in
41 for i := 0; i < N; i++ {
42 out := make(chan int)
43 go Chain(ch, i, in, out)
44 in = out
45 }
46 start <- 0
47 for i := 0; i < N; i++ {
48 ch <- i
49 }
50 <-in
51 }
52
53 func main() {
54 AsynchFifo()
55 SynchFifo()
56 }
57
View as plain text