Go "switch" and "break"

841 views
Skip to first unread message

Nguyên Nguyễn Văn Cao

unread,
Jul 12, 2013, 3:04:59 AM7/12/13
to golan...@googlegroups.com
I have this code: http://play.golang.org/p/O87jkcXZwM
package main
func main() {
for i := 0; i < 10; i++ {
switch i {
case 5:
println("you got", i)
break
}
println(i)
}
}
I think the "switch" don't need "break" for its "case" so I hope my "break" (at line 8) should be break the for loop not the switch but its not.
So, how do you guys think about that?

andrey mirtchovski

unread,
Jul 12, 2013, 3:08:56 AM7/12/13
to Nguyên Nguyễn Văn Cao, golang-nuts
here's the way to do what you want:

http://play.golang.org/p/ArGed8JXEU

that break will work anywhere. use it.

Jesse McNelis

unread,
Jul 12, 2013, 3:13:39 AM7/12/13
to Nguyên Nguyễn Văn Cao, golang-nuts
This is true it's a bit pointless, I believe the behaviour is due to avoiding bugs created by people that expect 'break' to work on the 'switch' since this is common in other languages.
 


--
=====================
http://jessta.id.au

Christian Neumann

unread,
Jul 12, 2013, 3:32:09 AM7/12/13
to golan...@googlegroups.com
On 12.07.2013 09:13, Jesse McNelis wrote:
> This is true it's a bit pointless, I believe the behaviour is due to
> avoiding bugs created by people that expect 'break' to work on the
'switch'
> since this is common in other languages.

As far as I understood, it's not pointless, as you can't conditionally
fall through without it:
http://play.golang.org/p/Ibxvm4soLL

break can be everywhere, fallthrough can only appear as last statement.


Christian

tomwilde

unread,
Jul 12, 2013, 6:25:36 AM7/12/13
to golan...@googlegroups.com, Nguyên Nguyễn Văn Cao, jes...@jessta.id.au
You can conditionally break out early on a switch statement:


that's why the break statement does apply to the switch even if there is no fallthrough by default.

Peter Nguyen

unread,
Jul 13, 2013, 3:33:01 AM7/13/13
to golan...@googlegroups.com
Would be nice with something like

break 2

to break in both the switch statement and the for loop.

gordon...@gmail.com

unread,
Jul 13, 2013, 7:41:03 AM7/13/13
to golan...@googlegroups.com
For that we have labels:

Jsor

unread,
Jul 13, 2013, 7:49:40 AM7/13/13
to golan...@googlegroups.com, gordon...@gmail.com
I've always found it obnoxious that gofmt decides to puts the loop label on the line above the loop, it seems confusing to me. The loop is no longer labelled, so it looks like I'm breaking out of empty space. It's obviously not a compiler/syntax concern because it compiles and runs just fine with the loop on the same line. Putting the label above the loop makes a certain amount of sense for using goto, less so for break/continue.
Message has been deleted

Nguyên Nguyễn Văn Cao

unread,
Jul 13, 2013, 10:56:32 AM7/13/13
to golan...@googlegroups.com, gordon...@gmail.com
Hehe, I don't know that I can use label for break and continue, so I do something stupid like this:

for {
  switch val {
  case 1:
    goto nextLoop
  case 2:
    goto endLoop
  default:
    //do something
  }
nextLoop:
}
endLoop:

Rémy Oudompheng

unread,
Jul 13, 2013, 10:59:35 AM7/13/13
to Nguyên Nguyễn Văn Cao, golang-nuts, Gordon Klaus
2013/7/13 Nguyên Nguyễn Văn Cao <nvc...@gmail.com>:
> Hehe, I don't know that I can use label for break and continue, so I do
> something stupid like this:
>

See the example in the spec: http://golang.org/ref/spec#Break_statements

Rémy.
Reply all
Reply to author
Forward
0 new messages