How to execute multiple query by using go routine

Having multiple goroutines running stuff and synchronizing them is a relatively easy problem to solve. Since you mentioned blocking until your two jobs are complete, check out sync.WaitGroup:

From the docs:

Other than the Once and WaitGroup types, most are intended for use by low-level library routines. Higher-level synchronization is better done via channels and communication.

So to that end, refer to the the tour of go concurrency section I linked to above that demonstrates channels. That said, I’d be willing to be you are prematurely optimizing. If I were you I’d just try running the queries and start thinking about concurrency only if you have a specific problem you want to solve.

1 Like