Skip to content

Instantly share code, notes, and snippets.

@ymotongpoo
Created November 27, 2012 05:58
Show Gist options
  • Select an option

  • Save ymotongpoo/4152616 to your computer and use it in GitHub Desktop.

Select an option

Save ymotongpoo/4152616 to your computer and use it in GitHub Desktop.
Channelを使ってのpipeline ref: http://qiita.com/items/0c61af9813b5039e21de
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