


An article to help you understand the basics of Go language concurrency (channel)
channel in Go, also called a pipeline, is used to share data# between multiple threads ##of. Normally, shared data in Go also uses
channel, but there are two a way to share data.
-
#Communicate through channels (recommended).
Why is shared memory communication not recommended?
Sample code: Multi-threading modifies a value.
Function
func Calc() { defer wg.Done() NUM = NUM - 1 }
main
var NUM = 100 var wg sync.WaitGroup func main() { for i := 0; i<100;i++ { wg.Add(1) go Calc() } wg.Wait() fmt.Println(NUM) }
Execution result
Yes, it is 2, you are confused, hahaha, the theory should be 0 ah.
Why is this?
This is why shared memory is not recommended. Our code is already multi-threaded.
In the first function code , line 3, NUM = NUM - 1.
如果多个线程同时执行到这一行,并且没有加锁,就会出现数据错乱。
那该怎么做呢?
加锁,加锁可以保证某一段代码只能被一个线程执行,防止被争抢。
代码
func Calc() { defer wg.Done() mutex.Lock() NUM = NUM - 1 mutex.Unlock() }
第3行加锁,第5行解锁。
执行结果
这次真的是0的,不管执行几次。
但是会发现一个问题,如果采用这种方式,需要常常注意竞争问题。
所以不是太推荐,需要考虑的比较多,并且各种加锁会消耗性能。
channel语法
channel格式
var 变量名 chan 类型 例如 var x1 chan int //x1管道里面只能存int类型数据 var x2 chan string //x2管道里面只能存字符串类型数据
注意
定义管道时,chan int
是一个整体,别搞错了各位。
创建channel
创建channel
,只能通过make
创建。
格式
var 变量名 = make(chan 类型,[管道大小]) 示例 var chan1 = make(chan int,10)//管道可以放10个int元素 var chan2 = make(chan string,5)//管道可以放5个string元素
channel操作
创建一个管道。
ch = make(chan int,10)
channel是一个管道,就像一个管子。
所以可以像管子里面塞东西,并且能取东西,关闭管道就是这个管道不能用了,里面的值取完就打样了。
像管子塞东西(发送)ch
。
从管子取东西(接收)var x =
。
Close the tubeclose(ch)
.
Note: channel is a first-in-first-out structure ,like this.
Precautions:
If the channel is full, it will be blocked if it is blocked again.
#If the channel is closed, no more value can be added, otherwise it will panic.
#Even if the channel is closed, the value can still be obtained until the value of the pipe is completely obtained. After the value is obtained, the zero value of the corresponding type is obtained.
The pipe cannot be closed repeatedly. Repeated closing will panic.
Unbuffered pipe
Unbuffered means that the pipe has no length. like this.
就像快读员没有快递柜,需要直接将快递给客户,如果没人要就撂摊子。
示例代码
package main import ( "fmt" ) //模拟张三 func 张三(x chan string) { var a = <-x fmt.Println(a) } func main() { //通道没有长度,就是无缓冲通道 var x = make(chan string) go 张三(x) x <- "张三的快递" fmt.Println("张三快递交付成功") }
第16行写入一个值,同理,张三就要等着去接,如果没人接,那就完了。
假设注释第9行代码。
直接报错,all goroutines are asleep - deadlock!
,这句话的意思是所有的协程都睡着了,死锁
无缓冲说明通道长度为0,发送一个值会阻塞住。
这就相当于快递员直接找张三,但是张三没了,但是快递员还得一直等着,等等等,然后挂了,终究还是没送出去。
有缓冲管道
这个就简单啦,多了一个快递柜,快递员直接将快递仍快递柜就行了。
示例代码
package main import ( "fmt" "sync" ) var wg sync.WaitGroup //快递员,快递员放10个快递 func 快递员(kuaidigui chan string) { defer wg.Done() for i := 0; i < 10; i++ { fmt.Println("快递员放入了第",i,"快递") kuaidigui <- fmt.Sprintf("第%d个快递", i) } //放完快递就关闭了通道 close(kuaidigui) } //张三,拿走3个快递 func 张三(kuaidigui chan string) { defer wg.Done() for i := 0; i < 3; i++ { fmt.Println("张三拿走" + <-kuaidigui) } } //李四拿走7个快递 func 李四(kuaidigui chan string) { defer wg.Done() for i := 0; i < 7; i++ { fmt.Println("李四拿走" + <-kuaidigui) } } func main() { //快递柜,10个大小 var 快递柜 = make(chan string, 10) wg.Add(3) go 快递员(快递柜) go 张三(快递柜) go 李四(快递柜) wg.Wait() }
执行结果
遍历channel两种方式
代码
func main() { //快递柜,10个大小 var ch = make(chan int, 10) //向管道中发送值 for i := 0; i < 10; i++ { ch <- i } //方式一取值 //for { //i, ok := <-ch ////取完值ok就是false //if !ok { // //结束循环 // break //} //fmt.Println(i) //} //方式二取值 for i:=range ch{ fmt.Println(i) } }
执行结果
报错是因为我在main中完成了发送值和取值两个操作,所以会出现上述问题,但是结果是没有错的。
One-way channel
We know that channel is possible Sending values and take the value , but in some scenarios for safety reasons, theoretically only Get the value, the latter can only send the value.
One-way channels are usually only reflected in function parameters.
Formal parameter chan
Writing only.Formal parameters
Read only.
#Modify the above courier code.
package main import ( "fmt" "sync" ) var wg sync.WaitGroup //快递员,快递员放10个快递,只写 chan<- string func 快递员(kuaidigui chan<- string) { defer wg.Done() for i := 0; i < 10; i++ { fmt.Println("快递员放入了第", i, "快递") kuaidigui <- fmt.Sprintf("第%d个快递", i) } //放完快递就关闭了通道 close(kuaidigui) } //张三,拿走3个快递,只读<-chan string func 张三(kuaidigui <-chan string) { defer wg.Done() for i := 0; i < 3; i++ { fmt.Println("张三拿走" + <-kuaidigui) } } //李四拿走7个快递 func 李四(kuaidigui <-chan string) { defer wg.Done() for i := 0; i < 7; i++ { fmt.Println("李四拿走" + <-kuaidigui) } } func main() { //快递柜,10个大小 var 快递柜 = make(chan string, 10) wg.Add(3) go 快递员(快递柜) go 张三(快递柜) go 李四(快递柜) wg.Wait() }
总结
上述讲述了Go语言并发如何和channel配合使用,毕竟我们一般的任务都不是单独运行的,都是互相配合的。
我们讲述了如何创建channel,如何使用channel,有缓冲管道和无缓冲管道区别,并且拒了一个快递员例子来展示协程和channel如何配合,最后用单向通道又加固了一下代码。
我的代码中使用了中文命名变量名是为了好看,实际开发中千万不要这样!!!
The above is the detailed content of An article to help you understand the basics of Go language concurrency (channel). For more information, please follow other related articles on the PHP Chinese website!

Go's strings package provides a variety of string manipulation functions. 1) Use strings.Contains to check substrings. 2) Use strings.Split to split the string into substring slices. 3) Merge strings through strings.Join. 4) Use strings.TrimSpace or strings.Trim to remove blanks or specified characters at the beginning and end of a string. 5) Replace all specified substrings with strings.ReplaceAll. 6) Use strings.HasPrefix or strings.HasSuffix to check the prefix or suffix of the string.

Using the Go language strings package can improve code quality. 1) Use strings.Join() to elegantly connect string arrays to avoid performance overhead. 2) Combine strings.Split() and strings.Contains() to process text and pay attention to case sensitivity issues. 3) Avoid abuse of strings.Replace() and consider using regular expressions for a large number of substitutions. 4) Use strings.Builder to improve the performance of frequently splicing strings.

Go's bytes package provides a variety of practical functions to handle byte slicing. 1.bytes.Contains is used to check whether the byte slice contains a specific sequence. 2.bytes.Split is used to split byte slices into smallerpieces. 3.bytes.Join is used to concatenate multiple byte slices into one. 4.bytes.TrimSpace is used to remove the front and back blanks of byte slices. 5.bytes.Equal is used to compare whether two byte slices are equal. 6.bytes.Index is used to find the starting index of sub-slices in largerslices.

Theencoding/binarypackageinGoisessentialbecauseitprovidesastandardizedwaytoreadandwritebinarydata,ensuringcross-platformcompatibilityandhandlingdifferentendianness.ItoffersfunctionslikeRead,Write,ReadUvarint,andWriteUvarintforprecisecontroloverbinary

ThebytespackageinGoiscrucialforhandlingbyteslicesandbuffers,offeringtoolsforefficientmemorymanagementanddatamanipulation.1)Itprovidesfunctionalitieslikecreatingbuffers,comparingslices,andsearching/replacingwithinslices.2)Forlargedatasets,usingbytes.N

You should care about the "strings" package in Go because it provides tools for handling text data, splicing from basic strings to advanced regular expression matching. 1) The "strings" package provides efficient string operations, such as Join functions used to splice strings to avoid performance problems. 2) It contains advanced functions, such as the ContainsAny function, to check whether a string contains a specific character set. 3) The Replace function is used to replace substrings in a string, and attention should be paid to the replacement order and case sensitivity. 4) The Split function can split strings according to the separator and is often used for regular expression processing. 5) Performance needs to be considered when using, such as

The"encoding/binary"packageinGoisessentialforhandlingbinarydata,offeringtoolsforreadingandwritingbinarydataefficiently.1)Itsupportsbothlittle-endianandbig-endianbyteorders,crucialforcross-systemcompatibility.2)Thepackageallowsworkingwithcus

Mastering the bytes package in Go can help improve the efficiency and elegance of your code. 1) The bytes package is crucial for parsing binary data, processing network protocols, and memory management. 2) Use bytes.Buffer to gradually build byte slices. 3) The bytes package provides the functions of searching, replacing and segmenting byte slices. 4) The bytes.Reader type is suitable for reading data from byte slices, especially in I/O operations. 5) The bytes package works in collaboration with Go's garbage collector, improving the efficiency of big data processing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
