Channel Directions {.en}

通道方向 {.zh}

::: {.en} When using channels as function parameters, you can specify if a channel is meant to only send or receive values. This specificity increases the type-safety of the program. :::

::: {.zh}

当使用通道作为函数的参数时,你可以指定这个通道是不是 只用来发送或者接收值。这个特性提升了程序的类型安全性。

:::

  1. package main
  2. import "fmt"

::: {.en} This ping function only accepts a channel for sending values. It would be a compile-time error to try to receive on this channel. :::

::: {.zh}

这个 ping 函数只接受发送值的通道。尝试在此通道上接收数据将会得到一个编译时错误。

:::

  1. func ping(pings chan<- string, msg string) {
  2. pings <- msg
  3. }

::: {.en} The pong function accepts one channel for receives (pings) and a second for sends (pongs). :::

::: {.zh}

pong 函数接受一个用于接收数据的通道(pings)和一个用于发送的通道(pongs)。

:::

  1. func pong(pings <-chan string, pongs chan<- string) {
  2. msg := <-pings
  3. pongs <- msg
  4. }
  5. func main() {
  6. pings := make(chan string, 1)
  7. pongs := make(chan string, 1)
  8. ping(pings, "passed message")
  9. pong(pings, pongs)
  10. fmt.Println(<-pongs)
  11. }
  1. $ go run channel-directions.go
  2. passed message