package main
import (
"fmt"
"sync"
)
func main() {
orderTotals := []int64{4200, 1590, 8800, 3050, 6675}
var totalRevenue int64
var wg sync.WaitGroup
for _, amount := range orderTotals {
wg.Add(1)
go func(cents int64) {
defer wg.Done()
totalRevenue += cents
}(amount)
}
wg.Wait()
fmt.Println(totalRevenue)
}
package main
import (
"fmt"
"sync"
"sync/atomic"
)
func main() {
orderTotals := []int64{4200, 1590, 8800, 3050, 6675}
var totalRevenue atomic.Int64
var wg sync.WaitGroup
for _, amount := range orderTotals {
wg.Add(1)
go func(cents int64) {
defer wg.Done()
totalRevenue.Add(cents)
}(amount)
}
wg.Wait()
fmt.Println(totalRevenue.Load())
}
5 worker goroutines each report an order total in cents; accumulate them into a shared total revenue counter, then print the final total.
expected_functional_output 24315