In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article analyzes "how to analyze Golang coprocess in go" with everyone. The content is detailed and easy to understand. Friends interested in "how to analyze Golang coordination in go" can read it slowly and deeply along with the ideas of Xiaobian. I hope it can help everyone after reading. Let's follow the small series to learn more about "how to analyze the Golang coprocess in go".
1. Hello Goroutine
package mainimport ( "fmt")
func main() {go sayHello()
}
func sayHello() {
fmt.Println("Hello Goroutine !!! ")
}
The go language starts the coroutine with the keyword go, followed by an anonymous function or a custom function, but if you run the above code, you will find that the terminal does not output 'Hello Gorounitn !!! 'Why?
The above code does not have a problem, it does not execute errors, and the reason why it does not output is because in golang, the main function is also a coroutine, and it is the main coroutine. What does that mean? That is, whenever you execute the go project (there must be a main function, and there can only be one main function), it will first open a main coroutine of the main function. The execution time of this main coroutine will affect the sub-coroutine. The sub-coroutine is the go keyword plus function. If the main coroutine is executed first, then the sub-coroutine will not be executed, so I changed the code to this:
package main
import (
"fmt"
"time"
)
func main() {
go sayHello()
time.Sleep(time.Second)
}
func sayHello() {
fmt.Println("Hello Goroutine !!! ")
}
Output:
Hello Goroutine !!!
sleep 1s, waiting for the subroutine to execute, so there will be output.
2. WaitGroup
Waiting for subroutines to execute in sleep does not conform to actual development, so WaitGroup under sync package can solve this problem, code:
package main
import (
"fmt"
"sync"
)
func main() {
wg := &sync.WaitGroup{}
wg.Add(2)
go sayHello(wg)
go sayHello2(wg)
wg.Wait()
}
func sayHello(wg *sync.WaitGroup) {
defer wg.Done()
fmt.Println("Hello Goroutine !!! ")
}
func sayHello2(wg *sync.WaitGroup) {
defer wg.Done()
fmt.Println("Hello Golang !!! ")
}
Explanation: Declare a wg variable in the main function, wg.Add(2) means I have two coroutines to execute here, so pass 2, what if you have n coroutines? Then pass n, wg.Done() equals wg.Add(-1), which means the subroutine is finished. wg.Wait() tells the main coroutine to wait for the child coroutine to finish executing before exiting.
3. Channel
Channel, also called pipeline, the pipeline in go is the channel of communication between coroutines, similar to our commonly used message queue.
package main
import (
"fmt"
"strconv"
)
func main() {
ch := make(chan string)
for i := 0; i = 10 {
close(ch)
}
i++
}
Because we clearly know that a total of 11 results will be output, so here simply make a judgment, greater than or equal to 10 to close the pipeline to exit the for loop, there will be no error! Although it does not conform to the actual development code specification, it can be used and can explain the principle.
Golang is a compiled language that compiles code into machine code, and the compiled binary can be deployed directly to the target machine without additional dependencies, so golang's performance is better than other interpreted languages, and goroutines can be used in golang to achieve concurrency, which provides a very elegant goroutine scheduler system that can easily generate millions of goroutines.
How to analyze Golang coordination in go is shared here. I hope the above content can improve everyone. If you want to learn more, please pay more attention to the updates of Xiaobian. Thank you for your attention to the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.