Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shockalotti/c1b0a2f63b27a5c7f2ec to your computer and use it in GitHub Desktop.
Save shockalotti/c1b0a2f63b27a5c7f2ec to your computer and use it in GitHub Desktop.
Go Golang - goroutines, channels, channel direction - ping pong example
package main
import (
"fmt"
"time"
)
func pinger(c chan string) {
for i := 0; ; i++ {
c <- "ping"
}
}
func ponger(c chan string) {
for i := 0; ; i++ {
c <- "pong"
}
}
func printer(c chan string) {
for {
msg := <- c
fmt.Println(msg)
time.Sleep(time.Second * 1)
}
}
func main() {
var c chan string = make(chan string)
go pinger(c)
go ponger(c)
go printer(c)
var input string
fmt.Scanln(&input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment