Skip to content

Instantly share code, notes, and snippets.

@micmania1
Last active June 8, 2018 04:52
Show Gist options
  • Save micmania1/d35a39220588f37bebacdc49c9318584 to your computer and use it in GitHub Desktop.
Save micmania1/d35a39220588f37bebacdc49c9318584 to your computer and use it in GitHub Desktop.
Go concurrency test (me learning go)
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Concurrency test")
concurrency := 5
workers := make(chan bool, concurrency)
// Ensure each worker is set to true (ready)
for i := 0; i < cap(workers); i++ {
select {
default:
workers <- true
}
}
// i is purely for output reasons
for i := 0; ; i++ {
select {
case <-workers:
output := make(chan string)
go func(w chan bool) {
defer func() {
w <- true
}()
output <- fmt.Sprintf("request %d", i)
time.Sleep(1000 * time.Millisecond)
}(workers)
fmt.Println(<-output)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment