Question: package main import ( fmt math / rand os strconv sync time ) func main ( ) {
package main
import
fmt
"mathrand
os
"strconv"
sync
"time"
func main
rand.SeedtimeNowUnixNano
if lenosArgs
fmtPrintlnPlease provide an integer argument."
return
num, err : strconv.AtoiosArgs
if err nil
fmtPrintlnInvalid input type."
return
buffered : makechan int, num buffered channel
var wait syncWaitGroup
wait.Add
go producer&wait, buffered, num
go consumerbuffered
wait.Wait
func producerwait syncWaitGroup, out chan int, num int
defer wait.Done
for x :; x num; x
value : rand.Intnnum
fmtPrintfProduced: v
value
out value
closeout
func consumerin chan int
for v : range in
fmtPrintfConsumed: v
v
simulate a producer and a consumer, each as a goroutine:
the producer produces a given number of random integers in a given range and sends each to the consumer via an unbuffered channel unidirectional or bidirectional
the producer goroutine outputs each number before it sends it
the consumer receives each integer from the producer and consumes it
the consumer goroutine outputs each number after it receives it
modify your program from part so that a buffered channel of a given size is used This is part of the program should have an output similar to Produced
Produced
Produced
Produced
Consumed
Consumed
Consumed
Consumed
Produced
Produced
Produced
Produced
Produced
Consumed
Consumed
Consumed
Consumed
Consumed
Produced
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
