Using WaitGroup and channels in Go


I’m working on a freight service using Go that call many webservices, but I’m not sure how can I implement the concurrency model. This aproach just worked, but sometimes it locks. I believe there are some issue with channels and WaitGroup. I realy need to use WaitGroups or only the channels are enouth to routines lock.

// Call carriers quote webservice
var wg sync.WaitGroup
error := make(chan error)
quote := make(chan []freight.Quote)
for _, c := range carriers {
    go c.Quote(&wg, obj, quote, error)

// Collect the results
quotes:= make([]freight.Quote, 0)
for i := 1; i < len(carriers); i++ {
    err := <-error
    quoteAws:= <-quote

    if err != nil {
    if quoteAws != nil {
        quotes= append(quotes, quoteAws...)

func (carrier CarrierA) Quote(wg *sync.WaitGroup, obj Volume, quotes chan []Quote, err chan error) 
    // Deal with waitgroup
    defer wg.Done()
    // Quote the freigth
    err <- nil
    quotes <- quotesResult


Use slices to collect the errors and quotes. Use a wait group to wait for the goroutines to complete.

errs := make([]error, len(carriers))
quotes := make([]freight.Quote, len(carriers))
var wg sync.WaitGroup
for i, c := range carriers {
    go func(i int, c Carrier) {
        defer wg.Done()
        quotes[i], errs[i] = c.Quote(args)
    }(i, c)

Answered By – Bayta Darell

Answer Checked By – Robin (GoLangFix Admin)

Leave a Reply

Your email address will not be published.