User Tools

Site Tools


ping-pong.go
package main
 
import (
    "fmt"
    "time"
)
 
func pinger0(c chan string) {
    for i := 0; ; i++ {
        c <- "ping0"
    }
}
 
func printer(c chan string) {
 
    for {
        msg := <- c
        fmt.Println(msg)
        time.Sleep(time.Second * 1)
    }
}
 
func pinger1(c chan string) {
    for i := 0; ; i++ {
        c <- "ping1"
    }
}
 
 
 
func main() {
    var c chan string = make(chan string, 5)
 
    go pinger0(c)
    go pinger1(c)
    go printer(c)
 
    var input string
    fmt.Scanln(&input)
}