Created
September 3, 2014 01:59
-
-
Save yukidarake/fbb87d6798bfe4411867 to your computer and use it in GitHub Desktop.
ワーカーを作って実行する例
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" | |
"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