channel directions
name chan string send&read
name chan <- string only send
name <-chan string only read
package main
import (
"fmt"
"time"
)
func worker(done chan bool) {
fmt.Println("working")
time.Sleep(time.Second)
fmt.Println("done")
done <- true
}
func ping(pings chan<- string, msg string) {
pings <- msg
}
func pong(pings <-chan string, pong chan<- string) {
msg := <-pings
pong <- msg
}
func main() {
pings := make(chan string, 1)
pongs := make(chan string, 1)
ping(pings, "pass message")
pong(pings, pongs)
defer close(pings)
defer close(pongs)
fmt.Println(<-pongs)
}