Skip to content

Instantly share code, notes, and snippets.

@sudix
Created January 27, 2015 01:09
Show Gist options
  • Save sudix/4a51d52e525afe404a13 to your computer and use it in GitHub Desktop.
Save sudix/4a51d52e525afe404a13 to your computer and use it in GitHub Desktop.
simple worker example. https://play.golang.org/p/rAx_z-s0M-
package main
import (
"fmt"
"sync"
)
func main() {
workers := 3
var wg sync.WaitGroup
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
for i, num := range data {
wg.Add(1)
go func(n int) {
fmt.Println("number:", n)
wg.Done()
}(num)
if i%workers == 0 {
fmt.Println("worker done")
wg.Wait()
}
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment