Workers

Report a typo

You have a channel that receives numbers. Data will come unevenly (at random intervals), and you do not know how many numbers will be on the input. Using three workers, organize their work in such a way that they read a number and add it to the variable counter. Workers finish their work when the channel is closed. Since there may be a data race for the counter variable in this task, configure the semaphore to allow each worker to access the variable in its own time interval.

Write a program in Go
package main

import (
"fmt"
"time"
)

func main() {
var dataChan = make(chan uint32) // DO NOT delete or modify this line of code

var counter uint32
?

workers := 3
for range make([]struct{}, workers) {
go func() {
// provide your solution here
}()
}

// DO NOT delete the lines below, they are used for checking the answer
writeData(dataChan)
time.Sleep(1 * time.Second)
fmt.Println("done")
check(counter)
}
___

Create a free account to access the full topic