zl程序教程

您现在的位置是:首页 >  后端

当前栏目

0101-Go-通道方向

Go 方向 通道
2023-09-27 14:23:45 时间

环境

  • Time 2022-08-24
  • Go 1.19

前言

说明

参考:https://gobyexample.com/channel-directions

目标

使用 Go 语言的通道,并且指定其方向。

示例

package main

import "fmt"

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

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

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

总结

使用 Go 语言的通道,并且指定其方向。

附录