首页 > 其他分享 >golang中的close函数

golang中的close函数

时间:2023-02-25 15:34:35浏览次数:29  
标签:ok 函数 chan golang 关闭 c3 close channel

close函数是用于关闭通道的。
官方解释(摘自close函数源代码注释):

The close built-in function closes a channel, which must be either
bidirectional or send-only. It should be executed only by the sender,
never the receiver, and has the effect of shutting down the channel after
the last sent value is received. After the last value has been received
from a closed channel c, any receive from c will succeed without
blocking, returning the zero value for the channel element. The form
x, ok := <-c
will also set ok to false for a closed channel.

翻译过来就是:

close函数是一个内建函数, 用来关闭channel,这个channel要么是双向的, 要么是只写的(chan<- Type)。
这个方法应该只由发送者调用, 而不是接收者。
当最后一个发送的值都被接收者从关闭的channel(下简称为c)中接收时,
接下来所有接收的值都会非阻塞直接成功,返回channel元素的零值。
如下的代码:
如果c已经关闭(c中所有值都被接收), x, ok := <- c, 读取ok将会得到false。

验证如下:

package main

import "fmt"

func main() {
  ch := make(chan int, 5)

  for i := 0; i < 5; i++ {
    ch <- i
  }

  close(ch) // 关闭ch
  for i := 0; i < 10; i++ {
    e, ok := <-ch
    fmt.Printf("%v, %v\n", e, ok)
  
    if !ok {
      break
    }
  }
}

输出:
0, true
1, true
2, true
3, true
4, true
0, false

在close之后, 还可以读取, 不过在读取完之后, 再检测ok, 就是false了。

注意事项:
对于值为nil的channel或者对同一个channel重复close, 都会panic, 关闭只读channel会报编译错误。, 代码示例如下:

1.关闭值为nil的通道

var c4 chan int

// 运行时错误:panic: close of nil channel
close(c4)
 

2.重复关闭同一个通道 c3 := make(chan int, 1) close(c3) // 运行时错误: // panic: close of closed channel close(c3)

3.关闭只读通道 c3 := make(<-chan int, 1) // 编译错误: // invalid operation: close(c3) (cannot close receive-only channel) close(c3) 正确的用法 c1 := make(chan int, 1) // 双向通道 (bidirectional) c2 := make(chan<- int, 1) // 只写的 (send-only) close(c1) close(c2)

  

欢迎补充指正! 

标签:ok,函数,chan,golang,关闭,c3,close,channel
From: https://www.cnblogs.com/gongxianjin/p/17154502.html

相关文章

  • 可变类型和不可变类型、闭包函数、装饰器+语法糖
    可变类型和不可变类型:  闭包函数:  装饰器+语法糖:   ......
  • golang 日志
    packagelogimport( "NOONASN/global" "github.com/natefinch/lumberjack" "go.uber.org/zap" "go.uber.org/zap/zapcore" "os" "path" "path/filepath")func......
  • map()函数应用
    title:map()函数应用author:杨晓东permalink:map()函数应用date:2021-10-0211:27:04categories:-投篮tags:-demomap()函数应用#正常函数一个参数d......
  • Golang Slice
    Golang—SliceSlice是Go语言中的一种数据类型,又称动态数组,依托数组实现,可以方便的进行扩容、传递等,实际使用中比数组更灵活。实现原理Slice依托数组实现,底层数组对......
  • [keil] 将函数定义到RAM运行,和定义无初始化变量(软复位,变量不清空)
    keil链接文件​​一、将函数定义到RAM运行​​​​二、定义无初始化变量(软复位,变量不清空)​​先打开Keil工程配置,选择linker链接文件,取消自动生成,并编辑sct。如上图,定义......
  • avformat_seek_file函数介绍
    在做音视频数据分析的时候,经常会遇到这样的需求,每隔5分钟抽取一帧数据进行分析。在做播放器开发的时候,也会遇到这种情况,就是拖动进度条跳转到某个位置进行播放。如果直接用......
  • 构造函数和析构函数
    类内的构造函数:相当于初始化函数,名字和类名一致,可以在里面写入初始化语句类内的析构函数类的对象调用完所有成员函数,将跳出程序之前释放内存空间,名字是构造函数......
  • 友元函数/类
    在类中添加友元,相当于安插了一个卧底,可以访问类内元素,如下classBox{doublewidth;public:friendvoidprintWidth(Boxbox);friendclassBigBox;......
  • c语言:辗转相除求最大公约数 函数
    #include<stdio.h>//求最大公约数:辗转相除法:辗转相除法是求两个自然数的最大公约数的一种方法,也叫欧几里德算法。//319377:319%377=319377%319=58319%58=2958%29=0......
  • C语言:更损相减法求最大公约数 函数应用
    #include<stdio.h>//<<九章算术>>更相减损法:可以用来求两个数的最大公约数,即“可半者半之,不可半者,副置分母、子之数,以少减多,更相减损,求其等也。//以等数约之。///第......