16. Concurrent Work
Activity 16.01: Listing Numbers
Solution:
- Create the
main.go
file and import the necessary packages:package main import ( "fmt" "log" "sync" )
- Define a function called
sum()
, which will use a pointer to a string to hold the result:func sum(from,to int, wg *sync.WaitGroup, res *string, mtx *sync.Mutex) { for i:=from;i<=to; i++ { mtx.Lock() *res = fmt.Sprintf("%s|%d|",*res, i) mtx.Unlock() } wg.Done() return }
- Create then the
main()
function to perform the sums:func main() { s1 := "" mtx := &sync.Mutex{} wg := &...