Created
January 16, 2014 04:56
-
-
Save quux00/8449996 to your computer and use it in GitHub Desktop.
Chinese Whispers example by Rob Pike http://www.youtube.com/watch?v=f6kdp27TYZs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// | |
// Chinese Whispers example by Rob Pike | |
// See http://www.youtube.com/watch?v=f6kdp27TYZs | |
// | |
func whisper(left, right chan int) { | |
left <- 1 + <- right | |
} | |
func main() { | |
const n = 100000 | |
leftmost := make(chan int) | |
right := leftmost | |
left := leftmost | |
for i := 0; i < n; i++ { | |
right = make(chan int) | |
go whisper(left, right) | |
left = right | |
} | |
go func(c chan int) { c <- 1 }(right) | |
fmt.Println(<- leftmost) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment