Skip to content

Instantly share code, notes, and snippets.

@rakeshopensource
Created February 24, 2018 17:33
Show Gist options
  • Save rakeshopensource/e2bd932c93d346a4f5acd708407c2321 to your computer and use it in GitHub Desktop.
Save rakeshopensource/e2bd932c93d346a4f5acd708407c2321 to your computer and use it in GitHub Desktop.
Producer Consumer problem implementation in golang
package main
import (
"fmt"
"strconv"
"time"
)
var done = make(chan string)
func producer(c chan<- string) {
for i := 1; i <= 10; i++ {
msg := strconv.Itoa(i)
c <- msg
fmt.Println("Produced \t #" + msg)
}
}
func consumer(c <-chan string) {
for {
time.Sleep(time.Second*1)
select {
case msg := <- c:
fmt.Println("Consumed \t #" + msg)
default:
done <- "true"
}
}
}
func main() {
queue := make(chan string, 1)
go producer(queue)
go consumer(queue)
<- done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment