Created
June 4, 2014 03:52
-
-
Save shockalotti/c1b0a2f63b27a5c7f2ec to your computer and use it in GitHub Desktop.
Go Golang - goroutines, channels, channel direction - ping pong example
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" | |
"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