Created
January 27, 2015 01:09
-
-
Save sudix/4a51d52e525afe404a13 to your computer and use it in GitHub Desktop.
simple worker example.
https://play.golang.org/p/rAx_z-s0M-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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