当前位置:网站首页>Go language channel understanding and use
Go language channel understanding and use
2022-06-11 00:18:00 【Game programming】
First of all, I read a paragraph from a guide before I wanted to write this article , It is mainly a paragraph that is easy to be misunderstood , Here I directly attach his code . You can also look at some basic knowledge first :
Catalog
Basics
effect : Used to transfer information between processes
type : The channel is a reference type , Rules are similar to slices , need make
Define ways and actions :
var ch chan int// The last one identifies the element type passed by the channel make(chan Element type , [ Buffer size ])ch <- n // Write n <- ch // read close(ch)// close Misunderstanding code
The core of the code is two threads and an unbuffered channel . Misunderstanding mainly occurs in the following basis notes ** After the channel is closed, the value can be taken ok=false**, For the unbuffered channel he set to exchange information , It can be right , But if there is a buffer channel , Is an obvious mistake . Below I attach a slightly modified code , The comparison between the two shows .
package main// Code 1 import ( "fmt" //"time")func main() { ch1 := make(chan int) ch2 := make(chan int) // Turn on goroutine take 0~100 The number of is sent to ch1 in go func() { for i := 0; i < 100; i++ { ch1 <- i } close(ch1) }() // Turn on goroutine from ch1 Received value in , And send the square of the value to ch2 in go func() { for { i, ok := <-ch1 // ** After the channel is closed, the value can be taken ok=false** if !ok { break } ch2 <- i * i } close(ch2) }() // In the main goroutine In the from ch2 Print the received value in for i := range ch2 { // When the channel is closed, it exits for range loop fmt.Println(i) }}This is a slightly modified code , You can see two changes , When overflowing, turn the channel 1 Replaced with buffered , Turn off the channel at one place 1 Time to i=50 When , Let's compare the execution results of the two :
Modify the code and compare the results
package mainimport ( "fmt" //"time")func main() { ch1 := make(chan int,100)// Changed to a buffered channel ch2 := make(chan int) // Turn on goroutine take 0~100 The number of is sent to ch1 in go func() { for i := 0; i < 100; i++ { ch1 <- i } }() // Turn on goroutine from ch1 Received value in , And send the square of the value to ch2 in go func() { for { i, ok := <-ch1 // After the channel is closed, the value can be taken ok=false if !ok { break } // Amendment if i == 50{ close(ch1) } ch2 <- i * i } close(ch2) }() // In the main goroutine In the from ch2 Print the received value in for i := range ch2 { // When the channel is closed, it exits for range loop fmt.Println(i) }}

You can see , Although the channel was closed in advance , But because there is a buffer , Characters are normally extracted , because
i, ok := <-ch1 As long as there is a value in the channel, it will return ok. Data cannot be added after closing , But the channel value is normal , Only when the element is fetched will it return false. In fact, the reason why it works is that there is no buffer channel , The receiving channel will be blocked until the sender transmits the value , There are no elements in the channel . So if you don't receive , You cannot add values to an unbuffered channel .
One way passage
Sometimes we will pass channels as parameters between multiple task functions , Many times we use channels in different task functions to restrict it , For example, the channel can only be sent or received in the function .
Go The language provides a one-way channel to handle this situation :
You can convert a two-way channel into a one-way channel in function parameters and any assignment operation .
package mainimport "fmt"// Write channel func counter(out chan<- int) { for i := 0; i < 100; i++ { out <- i } close(out)}func squarer(out chan<- int, in <-chan int) { for i := range in { out <- i * i } close(out)}// Read channel func printer(in <-chan int) { for i := range in { fmt.Println(i) }}func main() { ch1 := make(chan int) ch2 := make(chan int) go counter(ch1) go squarer(ch2, ch1) printer(ch2)}author :Chinatesila
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
猜你喜欢
![[pyGame games] here it is. This Gobang game is super A. share it with your friends~](/img/76/faea3558ed6fadff755c517922088b.png)
[pyGame games] here it is. This Gobang game is super A. share it with your friends~

【无标题】4555

【颜值检测神器】来,请拿出你们的绝活(这颜值,对得起观众么?)

Error report of curl import postman

Chapter I General introduction - Fundamentals of accounting

yum源更新
![[JVM] memory model](/img/01/4a9ab79e340f19c5f6cf682577bf2a.jpg)
[JVM] memory model
![[pyGame] stir up your brain and play the](/img/0c/fd558c843705af19720d790da4ff06.jpg)
[pyGame] stir up your brain and play the "24 o'clock" idea together ~ (awesome)
![[pyGame] can the little dinosaur on chrome be played with code? It looks like fun~](/img/b4/a4140eb10658af40a8a2fc0f428b0f.jpg)
[pyGame] can the little dinosaur on chrome be played with code? It looks like fun~
![[pyGame games] tank battle, how many childhood games do you remember?](/img/30/951fdbb944e026701af08c0c068cd8.png)
[pyGame games] tank battle, how many childhood games do you remember?
随机推荐
MySQL command line import and export data
Leetcode-560 and subarray with K
[database] MySQL index interview questions
Multipass中文文档-使用指引(目录页)
【漫天烟花】绚烂烟花点亮夜空也太美了叭、某程序员携带烟花秀给大家拜年啦~
【Pygame小游戏】来了来了它来了——这款五子棋小游戏超A的,分享给你的小伙伴儿一起pk吧~
WinDriver compilation summary
文件缓存和session怎么处理?
mysql 数据库 表 备份
【JVM】线程
【Pygame合集】回忆杀-“童年游戏”,看看你中几枪?(附五款源码自取)
[turtle confessions collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." Be happy for the rest of your life, and be safe for ever ~
[pyGame collection] please check the game guide through childhood: are there any games you have played? (attach five source codes for self access)
Select sort
VTK例子--三個相交的平面
The website is harmed by XSS hanging horse
Multipass中文文档-概览
Bluetooth development (11) -- ble interacts happily
Bluetooth (5) -- about retransmission
第一章 总论-会计基础