golang の特徴と言えば goroutine と channel ですが、その使いどころに悩む人もおられる様です。 goroutine は非同期に実行される処理、channel はその groutine と通信する為の仕組みと考えると分かりやすいです。 package main import ( "fmt" "time" ) func main() { task := make(chan string) taskquit := make(chan bool) workerquit := make(chan bool) go func() { loop: for { select { case <-taskquit: workerquit <- true break loop case job := <-task: fmt.Println(job) } } }() go func() {
![Big Sky :: Golang の channel の使い所](https://cdn-ak-scissors.b.st-hatena.com/image/square/ae5f360d3c82cd87e23b34fb6f5013ab7cf0f1a6/height=288;version=1;width=512/https%3A%2F%2Fmattn.kaoriya.net%2Fimages%2Flogo.png)