当前位置:网站首页>GO语言-管道channel
GO语言-管道channel
2022-06-26 12:41:00 【一边学习一边哭】
概述
通道channel可以被认为是goroutine通信的管道。关于前一篇go语言锁的博客中也提到,解决临界资源问题可以使用锁,但是更建议的是使用channel来实现goroutine之间的通信。
不要通过共享内存来通信,而应该通过通信来共享内存。
- 需要注意是,channel本身就是同步的。意味着同一时间只有一条goroutine来操作channel。
- 死锁——如果一个channel只有读数据,而没有写数据,则会产生死锁。反之亦然。如果写代码时不小心造成了死锁,go语言会抛出相关异常。
channel通道的创建和使用
channel声明
每个通道都有其相关的类型,该类型就是通道内允许传输的数据类型。
空的通道为nil,nil的通道没有任何用处。所以通道的声明是和map类似的。
//声明通道
var channel_name chan type
//初始化通道
channel_name = make(chan type)简短声明
channel_name := make(chan type)channel读写操作
data := <- channel_name //将通道中的值赋值给变量
channel_name <- data //将变量的值写入通道
value, ok := <- channel_name //从一个channel中读取值channel的读写操作都是阻塞的
正常情况下,如果不使用sync.WaitGroup同步等待组、锁,主函数中的子goroutine很可能在还没有执行时,因为主函数的结束而一起结束了。
但是可以通过“channel的读写操作都是阻塞的这一特性”,子goroutine通过channel通道传递信息给主goroutine,主goroutine在读到数据前会一直阻塞。
func main() {
channel1 := make(chan bool)
go func(){
for i:=0;i<10;i++{
fmt.Println(i)
}
channel1 <- true
fmt.Println("子goroutine结束...")
}()
data1 := <- channel1
fmt.Println("channel传递的值:",data1)
fmt.Println("主函数结束...")
}关闭channel通道
发送者可以通过关闭channel通道,告知接收方不会有更多的数据被发送到channel上了。
close(channel_name)接受者可以接受来自channel读到数据时,使用额外的变量来检查通道是否关闭。
ok为true时,表示从通道中读取了一个value;当ok是false是,意味着正从一个封闭的通道读取数据,读取到的value都将是零值。
value, ok := <- channel_namevar channel1 = make(chan int)
func main() {
go fun1()
for{
fmt.Println("从通道中读取值...")
v,ok := <-channel1
time.Sleep(1*time.Second)
if !ok {
fmt.Println("channel读取结束,通道关闭",v,ok)
break
}
fmt.Println("通道中的值:",v,ok)
}
}
func fun1() {
for i:=0;i<10;i++{
time.Sleep(1*time.Second)
fmt.Println("通道中写入值:", i)
channel1 <- i
}
close(channel1)
}channel通道的范围循环
通过for_range增强循环,不需要自己判断通道是否关闭。range会判断通道是否关闭。
var channel1 = make(chan int)
func main() {
go fun1()
for v := range channel1 { // v <- channel1
time.Sleep(1 * time.Second)
fmt.Println(v)
}
}
func fun1() {
for i := 0; i < 10; i++ {
channel1 <- i
}
close(channel1)
}缓冲通道
带缓冲区的通道,会把数据先写入缓冲区中。发送数据时,只有当缓冲区中满了才会被阻塞;接收数据时,只有缓冲区为空的时候才会被阻塞。
ch := make(chan type, Size)Size需要大于0,使通道具有缓冲区。默认情况无缓冲区通道的容量为0,所以省略了该参数。
var channel1 = make(chan int,5) //创建一个容量5的缓冲通道
func main() {
go fun1()
for v := range channel1 {
time.Sleep(1 * time.Second)
fmt.Println("\t读出数据",v)
}
}
func fun1() {
for i := 0; i < 10; i++ {
channel1 <- i
fmt.Println("写入数据:",i)
}
close(channel1)
}定向通道
双向通道
前面我们创建的通道,都是双向通道。双向通道是可以,一个goroutine发送数据,一个goroutine接收数据的。
定向通道(单向通道)
单向通道也就是定向通道。单向通道只能发送或者接收数据。
定向通道创建语法
channel1 := make(chan <-int) //只能写入数据,不能读数据
channel2 := make(<- chan int) //只能读数据,不能写入数据定向通道的用法
往往创建和使用通道的时候通常还是创建双向通道。单向通道只用来作为函数参数类型。
只是在将通道作为参数传递到函数时,函数中通道的类型使用单向通道。限制通道在函数内部使用时是只能读不能写的;或者是只能写不能读的。
边栏推荐
- KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
- Update and download of Beifu EtherCAT XML description file
- B - Bridging signals
- Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)
- ES中索引别名(alias)的到底有什么用
- 适配器模式(Adapter)
- Beifu cx5130 card replacement and transfer of existing authorization files
- 装饰器(Decorator)
- Digital signal processing -- Design of linear phase type (Ⅰ, Ⅲ) FIR filter (1)
- Beifu twincat3 can read and write CSV and txt files
猜你喜欢

Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias

Log in to the server using SSH key pair

mariadb学习笔记

awk工具

Mode pont

Common faults of MySQL database - forgetting database password

Echart stack histogram: add white spacing effect setting between color blocks

Learn how to develop owl components by hand (7): practical use of owl projects

IDC报告:百度智能云AI Cloud市场份额连续六次第一

What are the common categories of software testing?
随机推荐
MongoDB系列之Window环境部署配置
Enjoy element mode (flyweight)
Electron official docs series: Contributing
12 SQL optimization schemes summarized by old drivers (very practical)
shell脚本详细介绍(四)
Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program
Word document export (using fixed template)
2. Introduction to parallel interface, protocol and related chips (8080, 8060)
Reflect the technical depth (unable to speed up)
原型模式(prototype)
[how to connect the network] Chapter 1: the browser generates messages
MySQL数据库讲解(三)
sed编辑器
Analysis of state transition diagram of Beifu NC axis
Electron official docs series: Development
G - Cow Bowling
Map value
Oplg: new generation cloud native observable best practices
[how to connect the network] Chapter 2 (next): receiving a network packet
mysql讲解(一)