In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to understand goroutine, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
(1)。 Suppose there is a project that requires three engineering teams to complete, their construction events are independent of each other, who builds quickly and who finishes that part first, and the others do not need to wait for other engineering teams.
Package main
Import (
"fmt"
"time"
)
Func main () {
Fmt.Println ("start construction")
Go workTwo ()
Go workThree ()
Go workOne ()
Fmt.Println ("waiting for the total construction of the project to be completed")
Time.Sleep (time.Second * 3)
}
/ / workOne Engineering team 1 built part of it.
Func workOne () {
WorkTime: = time.Millisecond * 3000 / / time to complete the project
Time.Sleep (workTime)
Fmt.Println ("Construction completed: workOne")
}
/ / workTwo Engineering team 2 built part
Func workTwo () {
WorkTime: = time.Millisecond * 2500 / / time to complete the project
Time.Sleep (workTime)
Fmt.Println ("Construction completed: workTwo")
}
/ / workThree Engineering team 3 built part of it.
Func workThree () {
WorkTime: = time.Millisecond * 2000 / / time to complete the project
Time.Sleep (workTime)
Fmt.Println ("Construction completed: workThree")
}
(2)。 Add acceptance function to the project and use go chan to realize it.
Package main
Import (
"fmt"
"time"
)
Const (
WorkOneDone = "oneDone"
WorkTwoDone = "twoDone"
WorkThreeDone = "threeDone"
)
Func main () {
Fmt.Println ("start construction")
Ch: = make (chan string, 1)
Go func () {
WorkTwo (ch)
} ()
Go func () {
WorkThree (ch)
} ()
Go func () {
WorkOne (ch)
} ()
Go func () {
Test (ch)
} ()
/ / close chan
Defer close (ch)
/ / waiting for the completion of the general project
Time.Sleep (time.Second * 5)
}
Func test (ch chan string) {
For {
Select {
Case v: =
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.