Created
March 4, 2019 22:41
-
-
Save LordRahl90/1d972c508f181cc889aa9ed0756ac184 to your computer and use it in GitHub Desktop.
Producer consumer Implementation in go-lang
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" | |
func producer(p chan int, items int) { | |
defer close(p) | |
for i := 1; i <= items; i++ { | |
fmt.Println("Produer Producing ", i) | |
p <- i | |
} | |
} | |
func consumer(p chan int, done chan bool) { | |
defer close(done) | |
for val := range p { | |
fmt.Println("Consumer Consuming Item: ", val) | |
} | |
} | |
func main() { | |
i := 100 | |
p := make(chan int) | |
done := make(chan bool) | |
go producer(p, i) | |
go consumer(p, done) | |
<-done | |
fmt.Println("Everything is done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment