首页 > 其他分享 >0101-Go-通道方向

0101-Go-通道方向

时间:2022-10-30 08:44:56浏览次数:67  
标签:示例 0101 指定 方向 Go 通道

环境

  • 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 语言的通道,并且指定其方向。

附录

标签:示例,0101,指定,方向,Go,通道
From: https://www.cnblogs.com/jiangbo4444/p/16840475.html

相关文章

  • 0102-Go-通道选择器
    环境Time2022-08-24Go1.19前言说明参考:https://gobyexample.com/select目标使用Go语言的通道选择器。示例packagemainimport("fmt""time")......
  • 0103-Go-超时处理
    环境Time2022-08-24Go1.19前言说明参考:https://gobyexample.com/timeouts目标使用Go语言的通道选择器来处理超时。示例packagemainimport("fmt"......
  • 0083-Go-range 遍历
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/range目标使用Go语言的range遍历。切片求和packagemainimport"fmt"funcmain(){......
  • 0084-Go-函数
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/functions目标使用Go语言的函数。定义函数funcplus(aint,bint)int{returna+......
  • 0085-Go-多返回值函数
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/multiple-return-values目标使用Go语言的函数,返回两个值。直接返回packagemainimport"......
  • 0086-Go-可变参数函数
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/variadic-functions目标使用Go语言的可变参数函数。可变参数函数packagemainimport"fm......
  • 0087-Go-闭包
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/closures目标使用Go语言的闭包。示例packagemainimport"fmt"funcintSeq()func()i......
  • 0088-Go-递归
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/closures目标使用Go语言的递归。递归函数packagemainimport"fmt"funcfact(nint)i......
  • 0089-Go-指针
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/pointers目标使用Go语言的指针。示例packagemainimport"fmt"funczeroval(ivalint)......
  • 0090-Go-字符串
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/strings-and-runes目标使用Go语言的字符串。字节遍历packagemainimport"fmt"funcma......