Last Updated: March 02, 2016
·
813
· blakemesdag

Simple limited concurrency in Go

Limiting concurrency in go with WaitGroups is easy, here's a basic example. Each part is explained on my blog.

package main

import "sync"

func main() {
 var wg sync.WaitGroup
 wg.Add(1)

 go func(wg *sync.WaitGroup) {
   AccessALimitedResource()
   wg.Done()
 }(&wg)

 wg.Wait()
}

2 Responses
Add your response

Yes, this is a lot more useful than

time.Sleep(arbitraryNumber*time.Second)

over 1 year ago ·

You could also just use a boolean channel, which I believe to be a bit more idiomatic. http://play.golang.org/p/rGbUe7ljeh

over 1 year ago ·