1. 程式人生 > >Go Example--通道方向

Go Example--通道方向

package main

import "fmt"

func main() {
    pings := make(chan string, 1)
    pongs := make(chan string, 1)
    ping(pings, "passwd message")
    pong(pings, pongs)
    fmt.Println(<-pongs)
}

//只寫chan pings
func ping(pings chan<- string, msg string) {
    pings <- msg
}

//只讀chan pings
func pong(pings <-chan string, pongs chan<- string) {
    msg := <-pings
    pongs <- msg
}