Last active
January 29, 2016 13:50
-
-
Save esimov/dc5a365b66572d9d9a40 to your computer and use it in GitHub Desktop.
Ping-pong goroutine pattern
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 ( | |
"time" | |
"fmt" | |
) | |
func main() { | |
var ball int | |
ch := make(chan int) | |
for i:= 0; i < 10; i++ { | |
go func(){ | |
go PingPong(ch) | |
}() | |
} | |
ch <- ball | |
time.Sleep(1 * time.Second) | |
<-ch | |
} | |
func PingPong(ch chan int) { | |
for { | |
ball := <-ch | |
ball++ | |
fmt.Println(ball) | |
time.Sleep(1 * time.Second) | |
ch <- ball | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment