go vet not erroring waitgroup inc used after done

66 views
Skip to first unread message

Nikhilesh Susarla

unread,
Mar 25, 2024, 3:46:05 PM3/25/24
to golang-nuts
Hello, 

// main.go
package main

import (
"sync"
"time"
)

func main() {
var wg sync.WaitGroup

go lolsleep(&wg)
wg.Add(1)

wg.Wait()
}

func lolsleep(wg *sync.WaitGroup) {
wg.Done()
time.Sleep(3 * time.Second)
}



go vet main.go

Why isn't go vet complaining that wg.Add is happening after the usage. Where as the document clearly says increment and then use it as the wg.Done may be called an can run into negative/deadlock

Thank you

Ian Lance Taylor

unread,
Mar 25, 2024, 7:52:14 PM3/25/24
to Nikhilesh Susarla, golang-nuts
On Mon, Mar 25, 2024 at 12:46 PM Nikhilesh Susarla
<nikhil...@gmail.com> wrote:
>
> Why isn't go vet complaining that wg.Add is happening after the usage. Where as the document clearly says increment and then use it as the wg.Done may be called an can run into negative/deadlock

First, please send Go code as plain text or as a link to the Go
playground. The background on the text you sent makes it difficult to
read. Thanks.

To answer your question, vet doesn't check for any possible error
condition. As described at
https://cs.opensource.google/go/go/+/refs/tags/go1.22.1:src/cmd/vet/README,
vet checks for mistakes that are, among other things, common. I'm
not sure the mistake you describe is common.

Ian
Reply all
Reply to author
Forward
0 new messages