Skip to content

Instantly share code, notes, and snippets.

@torufurukawa
Created March 28, 2015 08:38
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 torufurukawa/bd5a5fb027306627488e to your computer and use it in GitHub Desktop.
Save torufurukawa/bd5a5fb027306627488e to your computer and use it in GitHub Desktop.
goroutine usage
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("hello")
q := make(chan int)
go producer(q)
go consumer(q)
go trigger(q)
time.Sleep(5 * time.Second)
fmt.Println("bye")
}
func producer(q chan int) {
fmt.Println("p:", "started")
var v int
for {
v = <-q
fmt.Println("p:", v)
}
fmt.Println("p:", "finished")
}
func consumer(q chan int) {
fmt.Println("c:", "started")
for i := 0; i < 5; i++ {
q <- i
time.Sleep(1 * time.Second)
}
fmt.Println("c:", "finished")
}
func trigger(q chan int) {
fmt.Println("t:", "started")
time.Sleep(3 * time.Second)
q <- 999
fmt.Println("t:", "finished")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment