Skip to content

Instantly share code, notes, and snippets.

@tanelih
Created January 13, 2015 09:06
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 tanelih/e3a5d10d265074cb9241 to your computer and use it in GitHub Desktop.
Save tanelih/e3a5d10d265074cb9241 to your computer and use it in GitHub Desktop.
Simple example of using a shared results channel to retrieve data.
package main
import "log"
import "time"
type (
DoneChannel chan int
ResultsChannel chan int
)
func GetStuffASync(id int, results ResultsChannel) DoneChannel {
done := make(DoneChannel)
go func() {
for i := 0; i < 10; i++ {
time.Sleep(time.Millisecond * 500)
results <- id
}
done <- id
return
}()
return done
}
func main() {
var (
done = []DoneChannel{}
results = make(ResultsChannel)
)
done = append(done, GetStuffASync(0, results))
done = append(done, GetStuffASync(1, results))
done = append(done, GetStuffASync(2, results))
go func() {
for _, d := range done {
log.Println("done", <-d)
}
close(results)
}()
for result := range results {
log.Println("result", result)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment