Created
December 16, 2017 14:11
-
-
Save jakelacey2012/ae9653b8807dcff6d9dcc4468dc77b13 to your computer and use it in GitHub Desktop.
go example of blocking buffered channels in go
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" | |
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