Goroutine and channel are two sharp swords of Golang's concurrency. If you want to master Golang, you need to learn these two sharp swords. Fortunately, this is not difficult, I just wanted to understand it after sending two takeaways🐶.
Introduction
Goroutine is called Go coroutine, and a coroutine can be created through the go keyword.
go func() {
// do
}()
Channel is called a channel and is created by the make key.
ch := make(chan T)
// or
ch := make(chan T, n)
Channel is divided into two types: unbuffered and buffered. For buffered channels, the buffer size can be set through the second parameter of make.
Goroutine + Unbuffered Channel
In concurrency, multiple Goroutines communicate with each other, which requires the help of channels.
The following sample code, start two coroutines, representing " point takeaway you " and " outgoing buy me ", we were busy with their own affairs in their respective Association process. We communicate through channels (gates).
For an unbuffered channel, I need to pass an unbuffered channel (gate) to send the message (chicken leg takeaway) to you. channel of 161b82bdfbc2e6 means that I have to deliver the drumsticks to your hands by myself, instead of hanging the drumsticks on the doorknob and other things 161b82bdfbc2e7.
ch <- food
: I am stuck here at this time, and I have to wait here before you chir open the door.
After a while, you are done with other things, come to the door to get the chicken drumstick food := <- ch
.
After I hand the chicken drumsticks to you, it will no longer be blocked and I can continue to send other orders. You get the takeaway and start eating chicken.
If once, you are too hungry, and you happen to be busy with everything at hand, you will come to the door and wait for the takeaway. When my takeaway is not delivered, you need to block here and wait for the takeaway. You can only start eating if I knock on the door and pass the chicken drumstick to you.
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan string)
go func() {
who := "外卖小哥我:"
food := "鸡腿"
fmt.Println(who, "送餐中……2s")
time.Sleep(time.Second * 2)
fmt.Println(who, "已送餐到门口,等待顾客开门取餐")
ch <- food
fmt.Println(who, "订单已送达,开始送其他单")
}()
go func() {
who := "你:"
fmt.Println(who, "等待外卖……")
time.Sleep(time.Second * 3)
fmt.Println(who, "磨磨唧唧开门中……3s")
food := <- ch
fmt.Println(who, "拿到", food, "开吃!")
}()
time.Sleep(time.Second * 5)
}
Goroutine + Buffer Channel
The unbuffered channel forced me to block and affected the efficiency of my delivery. I decided to use buffer capacity of 1 to communicate with you to improve my delivery efficiency.
So, when I come to your door, I can hang the chicken drumstick on the door handle ( ch <- food
), this is not my blocking, and then I can send other orders. When you are done, you can open the door to pick up the meal at any time ( food := <- ch
). In this way, you and I don't have to wait for each other because of other people's affairs. In parallel, you and I are able to share data through channels when we are implementing ourselves step by step. This is the buffer channel.
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan string, 1)
go func() {
who := "外卖小哥我:"
food := "鸡腿"
fmt.Println(who, "送餐中……2s")
time.Sleep(time.Second * 2)
fmt.Println(who, "已送餐到门口,餐挂门把手了(缓冲区)")
ch <- food
fmt.Println(who, "订单已送达,开始送其他单")
}()
go func() {
who := "你:"
fmt.Println(who, "等待外卖……")
time.Sleep(time.Second * 3)
fmt.Println(who, "磨磨唧唧开门中……3s")
food := <- ch
fmt.Println(who, "拿到", food, "开吃!")
}()
time.Sleep(time.Second * 5)
}
Summarize
The unbuffered channel requires me to hand the chicken legs to your hands, and the buffered channel means I can hang the chicken legs on the door handle and wait for you to pick it up when you need it.
Seeing here, you have understood Goroutine + Channel. Next, I will leave a thought question. I believe that if you understand it, you will be able to make it~
Thinking Question: Please execute the following output result
func main() {
ch := make(chan string, 1)
go func() {
who := "送完外卖的我:"
time.Sleep(time.Second)
fmt.Println(who, "writing……1 hour")
ch <- "文章"
}()
go func() {
who := "你:"
fmt.Println(who, "摸鱼……")
read := <- ch
fmt.Println(who, "获得阅读", read, ", 并点赞")
}()
time.Sleep(time.Second * 5)
}
The article is from . I sent two takeaways. I want to understand Goroutine + Channel
This article is published by the blog one article multi-posting OpenWrite
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。