Created
December 2, 2014 00:55
-
-
Save sideb0ard/2853464c2e3245d2e33a to your computer and use it in GitHub Desktop.
Boring go from Rob Pike, Google IO 2012
This file contains 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" | |
//"math/rand" | |
//"time" | |
) | |
type Message struct { | |
str string | |
wait chan bool | |
} | |
func f(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 f(left, right) | |
left = right | |
} | |
go func(c chan int) { c <- 1 }(right) | |
fmt.Println(<-leftmost) | |
} | |
// generator pattern | |
func boring(msg string, quit chan string) <-chan string { | |
c := make(chan string) | |
//waitForIt := make(chan bool) | |
// function literal | |
go func() { | |
for i := 0; ; i++ { | |
select { | |
case c <- fmt.Sprintf("%s: %d", msg, i): | |
case <-quit: | |
quit <- "See you1!" | |
return | |
} | |
} | |
}() | |
return c | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment