A pit that you may encounter when you use Select with for in the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

As a new bird that has just come into touch with go programming, it is not intimidated by some of its new features, especially when it has become accustomed to C + + programming. For go language, on-line mixed, there are highly respected, such as seven cattle Xu Xiwei, its evaluation of GO is very high, and predicted go market share will have been gradually rising, replace Java, become the first. There are also hard to belittle, they will generally have a variety of blog posts on the network, detailed analysis of the go to the wrong place. And I touch go this time, I have to say, it is really a very interesting language, why with interesting description, borrowing a word of force, the existence is reasonable. Go can slowly be domestic and foreign major Internet companies for the server architecture, it has its own flash place, and as for the interesting, it needs to be interested in the people slowly to find, (er .... This scene is a bit long, seemingly a bit of a deviation)

Ok, so to speak, recently in the process of using go to develop a project, suddenly found a pit, especially for other traditional language transferred to the people who do not pay attention to fall out of the pit, say no more, show you the code.

Func Test () {i: = 0 for {select {case <-time. After (time. Second * time. Duration (2)): i++ if i = = 5{FMT. Println ("Break Now") break} FMT. Println ("Inside the Select:")} FMT. Println ("Inside the For:")}}
After execution found, what? Can't even break out? And then we checked the data. When the for and select are used together, the break language cannot jump out of the for, so to break out, you need to add a label, use Goto, or break to a specific location

Workaround One: Use the feature of break in Golang to add a label to the outer layer for

Func Test () {i: = 0forend:for {select {case <-time. After (time. Second * time. Duration (2)): i++if i = = 5{fmt. Println ("Break Now") forend}fmt. Println ("Inside the Select:")}fmt. Println ("Inside the For:")}}

Workaround two: Use Goto to jump directly out of the loop

Func Test () {i: = 0for {select {case <-time. After (time. Second * time. Duration (2)): i++if i = = 5{fmt. Println ("Break Now") Goto FOREND}FMT. Println ("Inside the Select:")}fmt. Println ("Inside the For:")}forend:}

As above, succeed out of the pit.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.