Reputation: 1196
Too often, reading at Go source code, I encounter this pattern where a value is received from a channel, something such as <-doneC
, it does not save the result to a variable.
I don't understand what it does.
doneC, _, err := rest.XReceive(eHandler, errHandler)
if err != nil {
fmt.Println("Error",err)
return
}
<-doneC
Upvotes: 3
Views: 582
Reputation: 85481
That's an idiomatic way to implement an asynchronous wait in Go.
A function starts a new goroutine and returns a "done" channel to the caller.
The caller then does <-doneC
which is basically an attempt to receive from the channel, ignoring the result.
The goroutine, when finished, can then either send a dummy value to doneC
or, better yet, simply close
it. That acts as the signal for <-doneC
to resume execution.
The added benefit of closing doneC
instead of sending a dummy value is that (1) multiple calls to <-doneC
can be unblocked at the same time, and (2) if none are waiting, the sending will block, but close
won't.
Upvotes: 6