Skip to content

Instantly share code, notes, and snippets.

@yukidarake
Created September 3, 2014 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukidarake/fbb87d6798bfe4411867 to your computer and use it in GitHub Desktop.
Save yukidarake/fbb87d6798bfe4411867 to your computer and use it in GitHub Desktop.
ワーカーを作って実行する例
package main
import (
"fmt"
"log"
"sync"
)
func worker(msg string) <-chan string {
var wg sync.WaitGroup
receiver := make(chan string)
go func() {
for i := 0; i < 3; i++ {
wg.Add(1)
go func(i int) {
msg := fmt.Sprintf("%d %s done", i, msg)
receiver <- msg
wg.Done()
}(i)
}
wg.Wait()
close(receiver)
}()
return receiver
}
func main() {
receiver := worker("job")
for {
receive, ok := <-receiver
if !ok {
return
}
log.Println(receive)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment