Skip to content

Instantly share code, notes, and snippets.

@xsaamiir
Created January 29, 2019 08:51
Show Gist options
  • Save xsaamiir/62d4fec3de1474ef9d8b5b8d55943bcc to your computer and use it in GitHub Desktop.
Save xsaamiir/62d4fec3de1474ef9d8b5b8d55943bcc to your computer and use it in GitHub Desktop.
Golang goroutines semaphores
package main
import "fmt"
// sem is a channel that will allow up to 10 concurrent operations.
var sem = make(chan int, 10)
func main() {
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