Created
February 7, 2016 16:57
-
-
Save alexchowle/ef350894ec0c3d10aa53 to your computer and use it in GitHub Desktop.
Simple golang pingpong
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() { | |
pingChan := make(chan string) | |
pongChan := make(chan string) | |
go printer(pongChan) | |
go pinger(pingChan) | |
go ponger(pingChan, pongChan) | |
// Need to block until all complete | |
var input string | |
fmt.Scanln(&input) | |
} | |
func pinger(pingChan chan<- string) { | |
// Send a ping | |
fmt.Println("pinger sending \"ping\"") | |
pingChan <- "ping" | |
} | |
func ponger(pingChan <-chan string, pongChan chan<- string) { | |
// receive the ping | |
fmt.Println("ponger received", <-pingChan) | |
// respond with a pong | |
fmt.Println("ponger replying with \"pong\"") | |
pongChan <- "pong" | |
} | |
func printer(pongChan <-chan string) { | |
// read the pong | |
fmt.Println("printer received", <-pongChan) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment