Created
March 28, 2015 08:38
-
-
Save torufurukawa/bd5a5fb027306627488e to your computer and use it in GitHub Desktop.
goroutine usage
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" | |
"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