Skip to content

Instantly share code, notes, and snippets.

@sideb0ard
Created December 2, 2014 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sideb0ard/2853464c2e3245d2e33a to your computer and use it in GitHub Desktop.
Save sideb0ard/2853464c2e3245d2e33a to your computer and use it in GitHub Desktop.
Boring go from Rob Pike, Google IO 2012
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