Question: package main import ( fmt math / rand os strconv sync time ) func main ( ) {

package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"sync"
"time"
)
func main(){
rand.Seed(time.Now().UnixNano())
if len(os.Args)<2{
fmt.Println("Please provide an integer argument.")
return
}
num, err := strconv.Atoi(os.Args[1])//convert to int
if err != nil {
fmt.Println("Invalid input type.")
return
}
unbuffered := make(chan int)//unbuffered channel
var wait sync.WaitGroup
wait.Add(1)//begin incrementing the counter
go producer(&wait, unbuffered, num)
go consumer(unbuffered)
wait.Wait()
}
func producer(wait *sync.WaitGroup, out chan<- int, num int){
defer wait.Done()
for x :=0; x < num; x++{
value := rand.Intn(num)
fmt.Printf("Produced: %v
", value)
out <- value
}
close(out)
}
func consumer(in <-chan int){
for v := range in {
fmt.Printf("Consumed:%v
", v)
}
} Write the following code using a bufffered channel instead of an unbuffrered channel in GOLANG

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!