Skip to content

Instantly share code, notes, and snippets.

@dearing
Created February 23, 2019 08:31
Show Gist options
  • Save dearing/789e5b9105818bb370bee775f6c8ff5c to your computer and use it in GitHub Desktop.
Save dearing/789e5b9105818bb370bee775f6c8ff5c to your computer and use it in GitHub Desktop.
package main
import (
"log"
"math/rand"
"sync"
"time"
"github.com/google/uuid"
)
func main() {
log.Println("Hello World")
r := rand.New(rand.NewSource(99))
var wg sync.WaitGroup
count := 0
in := make(chan string, 5)
// create 5 workers to consume a channel
for i := 0; i < 20; i++ {
wg.Add(1)
count++
go func(name int, in chan string) {
defer wg.Done()
completed := 0
for val := range in {
completed++
sleep := time.Duration(r.Intn(60)) * time.Second
time.Sleep(sleep)
log.Printf("goroutine-%02d-%02d: %s took %s", name, completed, val, sleep.String())
}
}(count, in)
}
// generate random strings because I am lazy
for index := 0; index < 10000; index++ {
in <- uuid.New().String()
}
close(in)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment