Last active
April 8, 2018 11:20
-
-
Save just1689/3d254bda346e004ba30b2ea68d325310 to your computer and use it in GitHub Desktop.
Go routines communicating back and forth
This file contains 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" | |
func main() { | |
channel := make(chan int) | |
done := make(chan bool) | |
start := 0 | |
go playerOne(channel, done) | |
go playerTwo(channel, done) | |
channel <- start | |
<- done | |
} | |
func playerOne(c chan int, done chan bool) { | |
for { | |
next := <-c | |
fmt.Println("PING: ", next) | |
next++ | |
if next > 10 { | |
close(c) | |
close(done) | |
return | |
} | |
c <- next | |
} | |
} | |
func playerTwo(c chan int, done chan bool) { | |
for { | |
next := <-c | |
fmt.Println("PONG: ", next) | |
next++ | |
if next > 10 { | |
close(c) | |
close(done) | |
return | |
} | |
c <- next | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment