Skip to content

Instantly share code, notes, and snippets.

@fzerorubigd
Created March 6, 2015 14:13
Show Gist options
  • Save fzerorubigd/bff38fce80a3fde1698c to your computer and use it in GitHub Desktop.
Save fzerorubigd/bff38fce80a3fde1698c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
)
type job struct {
id int
url string
}
const total int = 100
func main() {
c := make(chan job)
done := make(chan struct{})
wg := sync.WaitGroup{}
go func() {
for {
resp := <-c
go func() {
// Do the job
fmt.Println(resp.id, "=>", resp.url)
// The job is done!
wg.Done() // make sure do this before writing in channel
done <- struct{}{}
}()
}
}()
jobs := make([]job, total)
// Fill the jobs here
for i := range jobs {
jobs[i] = job{id: i, url: fmt.Sprintf("the url number %d", i)}
}
//now pass the job to worker
for i := range jobs {
wg.Add(1)
if i < 10 { // 10 is the limit
c <- jobs[i]
} else {
c <- jobs[i]
<-done
}
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment