Skip to content

Instantly share code, notes, and snippets.

@jakelacey2012
Created December 16, 2017 14:11
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 jakelacey2012/ae9653b8807dcff6d9dcc4468dc77b13 to your computer and use it in GitHub Desktop.
Save jakelacey2012/ae9653b8807dcff6d9dcc4468dc77b13 to your computer and use it in GitHub Desktop.
go example of blocking buffered channels in go
package main
import "fmt"
import "time"
func write(channel chan int) {
for i := 0; i < 5; i++ {
channel <- i
fmt.Println("successfully wrote", i, "to channel")
}
close(channel)
}
func main() {
ch := make(chan int, 2)
go write(ch)
time.Sleep(2 * time.Second)
for v := range ch {
fmt.Println("read value", v, "from channel")
time.Sleep(2 * time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment