Created
January 17, 2019 15:10
-
-
Save QuynhVir/6fbd06be4ddcd3e5524f1936d9865e85 to your computer and use it in GitHub Desktop.
Limit number of goroutines running
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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