Go's New WaitGroup.Go
dev.to·14w·
Discuss: DEV

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...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help