Sobes.tech
Senior

What does the Go convention say about using WaitGroup and return types (accept interfaces, return concrete types)?

sobes.tech AI

Answer from AI

Go conventions recommend using interfaces in functions to ensure flexibility and the ability to substitute implementations, while returning concrete types for simplicity and predictability.

In the context of WaitGroup, this means that you typically create and manage a specific type sync.WaitGroup, rather than an interface, since it has a specific set of methods and does not require abstraction.

Example:

var wg sync.WaitGroup
wg.Add(1)
go func() {
    defer wg.Done()
    // work
}()
wg.Wait()

Here, we use the concrete type sync.WaitGroup, not an interface, because it is simpler and aligns with Go idioms.

What does the Go convention say about using WaitGroup… - sobes.tech