Created
August 15, 2014 08:31
-
-
Save bitristan/f48c924264e86897368b to your computer and use it in GitHub Desktop.
golang consumer and producer pattern
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 | |
/* producer-consumer problem in Go */ | |
import ("fmt") | |
var done = make(chan bool) | |
var msgs = make(chan int) | |
func produce () { | |
for i := 0; i < 10; i++ { | |
msgs <- i | |
} | |
done <- true | |
} | |
func consume () { | |
for { | |
msg := <-msgs | |
fmt.Println(msg) | |
} | |
} | |
func main () { | |
go produce() | |
go consume() | |
<- done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment