Created
February 16, 2022 12:51
-
-
Save yuseferi/cc4f865219fdcfff9c1dcd632ac19965 to your computer and use it in GitHub Desktop.
Producer Consumer basic in Golang
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 | |
import ( | |
"flag" | |
"fmt" | |
) | |
type Consumer struct { | |
msgs chan int | |
} | |
// NewConsumer creates a Consumer | |
func NewConsumer(msgs chan int) *Consumer { | |
return &Consumer{msgs: msgs} | |
} | |
// consume reads the msgs channel | |
func (c *Consumer) consume() { | |
fmt.Println("consume: Started") | |
for { | |
msg := <-c.msgs | |
fmt.Println("consume: Received:", msg) | |
} | |
} | |
// Producer definition | |
type Producer struct { | |
msgs *chan int | |
done *chan bool | |
} | |
// NewProducer creates a Producer | |
func NewProducer(msgs *chan int, done *chan bool) *Producer { | |
return &Producer{msgs: msgs, done: done} | |
} | |
// produce creates and sends the message through msgs channel | |
func (p *Producer) produce(max int) { | |
fmt.Println("produce: Started") | |
for i := 0; i < max; i++ { | |
fmt.Println("produce: Sending ", i) | |
*p.msgs <- i | |
} | |
*p.done <- true // signal when done | |
fmt.Println("produce: Done") | |
} | |
func main() { | |
// get the maximum number of messages from flags | |
max := flag.Int("n", 5, "defines the number of messages") | |
flag.Parse() | |
var msgs = make(chan int) // channel to send messages | |
var done = make(chan bool) // channel to control when production is done | |
// Start a goroutine for Produce.produce | |
go NewProducer(&msgs, &done).produce(*max) | |
// Start a goroutine for Consumer.consume | |
go NewConsumer(msgs).consume() | |
go NewConsumer(msgs).consume() | |
// Finish the program when the production is done | |
<-done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment