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 := &...