Created
November 27, 2012 05:58
-
-
Save ymotongpoo/4152616 to your computer and use it in GitHub Desktop.
Channelを使ってのpipeline ref: http://qiita.com/items/0c61af9813b5039e21de
This file contains hidden or 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" | |
| func main() { | |
| queue := make(chan string) | |
| out := make(chan int) | |
| foo := []string{"foo","bar","baz","spam","egg","ham"} | |
| var bar = map[string]int{ | |
| "foo": 1, | |
| "bar": 2, | |
| "baz": 100, | |
| } | |
| var pipeline = func(in chan string, out chan int) { | |
| for { | |
| req, ok := <-in | |
| if !ok { | |
| break | |
| } | |
| if bar[req] != 0 { | |
| out <- bar[req] | |
| } else { | |
| fmt.Printf("pipeline --> %s:\n", req) | |
| } | |
| } | |
| close(out) | |
| } | |
| go func() { | |
| for i:=0; i<1000; i++ { | |
| queue <- foo[i%len(foo)] | |
| } | |
| close(queue) | |
| }() | |
| go pipeline(queue, out) | |
| for { | |
| req, ok := <-out | |
| if !ok { | |
| break | |
| } | |
| fmt.Printf("final --> %d\n", req) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment