Go's New WaitGroup.Go
dev.to·17h·
Discuss: DEV
Flag this post

Go 1.25+ added a new method to the sync package that removes common boilerplate: the WaitGroup.Go() method. If you’ve used wg.Add(1), go func(), and defer wg.Done() hundreds of times, this one’s for you.

The old way

Here’s how we launch concurrent goroutines:

package main

import (
"fmt"
"sync"
)

func main() {
var wg sync.WaitGroup
list1 := []int{10, 20, 30, 40}
list2 := []int{1, 10, 20, 30, 40}

l1 := []int{-1, 4, 5, 6}
l2 := []int{4, 5, 6}

wg.Add(1)
go func() {
defer wg.Done()
solution(list2, list1)
}()

wg.Add(1)
go func() {
defer wg.Done()
solution(l2, l1)
}()

wg.Wait()
}

func solution(x, y []int) {
var uniqueIdx int
for _, num1 := range x {
uniqueIdx ^= num1
}
for _, num2 := range y {
uniqueIdx ^= num2
}
fmt.Println(uniqueIdx)
}

This works fine. But…

Similar Posts

Loading similar posts...