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.