Skip to content

Instantly share code, notes, and snippets.

@just1689
Last active April 8, 2018 11:20
Show Gist options
  • Save just1689/3d254bda346e004ba30b2ea68d325310 to your computer and use it in GitHub Desktop.
Save just1689/3d254bda346e004ba30b2ea68d325310 to your computer and use it in GitHub Desktop.
Go routines communicating back and forth
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