Created
February 24, 2018 17:33
-
-
Save rakeshopensource/e2bd932c93d346a4f5acd708407c2321 to your computer and use it in GitHub Desktop.
Producer Consumer problem implementation in golang
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" | |
"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