Skip to content

Instantly share code, notes, and snippets.

@ebenoist
Last active June 21, 2016 01:22
Show Gist options
  • Save ebenoist/efe4a6fb427064ddcccc9e6ce10e9eef to your computer and use it in GitHub Desktop.
Save ebenoist/efe4a6fb427064ddcccc9e6ce10e9eef to your computer and use it in GitHub Desktop.
ch9.go
package main
import (
"fmt"
"os"
"strconv"
"time"
)
func main() {
num, _ := strconv.Atoi(os.Args[1])
first := make(chan bool)
out := make(chan bool)
go flipper(first, out)
then := time.Now()
fmt.Printf("Building a chain with %d goroutines\n", num)
var in chan bool
for i := 0; i < num; i++ {
in = out
out = make(chan bool)
go flipper(in, out)
}
fmt.Printf("Chain built in %v\n", time.Now().Sub(then))
then = time.Now()
first <- true
<-out
fmt.Printf("Sent finished in %v\n", time.Now().Sub(then))
}
func flipper(in chan bool, out chan bool) {
r := <-in
out <- !r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment