Skip to content

Instantly share code, notes, and snippets.

@alexchowle
Created February 7, 2016 16:57
Show Gist options
  • Save alexchowle/ef350894ec0c3d10aa53 to your computer and use it in GitHub Desktop.
Save alexchowle/ef350894ec0c3d10aa53 to your computer and use it in GitHub Desktop.

Revisions

  1. Alex Howle revised this gist Feb 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pingpong.go
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ func pinger(pingChan chan<- string) {
    }

    func ponger(pingChan <-chan string, pongChan chan<- string) {
    // receive and discard the ping
    // receive the ping
    fmt.Println("ponger received", <-pingChan)

    // respond with a pong
  2. Alex Howle created this gist Feb 7, 2016.
    38 changes: 38 additions & 0 deletions pingpong.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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 and discard 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)
    }