Skip to content

Instantly share code, notes, and snippets.

@QuynhVir
Created January 17, 2019 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuynhVir/6fbd06be4ddcd3e5524f1936d9865e85 to your computer and use it in GitHub Desktop.
Save QuynhVir/6fbd06be4ddcd3e5524f1936d9865e85 to your computer and use it in GitHub Desktop.
Limit number of goroutines running
package main
import "fmt"
const MAX = 20
func main() {
sem := make(chan int, MAX)
for {
sem <- 1 // will block if there is MAX ints in sem
go func() {
fmt.Println("hello again, world")
<-sem // removes an int from sem, allowing another to proceed
}()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment