Hi threre!
Using continue Label
, break Label
- is it good practice?
It looks similar to using goto Label
.
Is it normal to use it? Or I need to beware of it.
Hi threre!
Using continue Label
, break Label
- is it good practice?
It looks similar to using goto Label
.
Is it normal to use it? Or I need to beware of it.
continue label
and break label
are part of the syntax. They are not the same as a goto label
.
Maybe this article will help: https://medium.com/golangspec/labels-in-go-4ffd81932339
Yes, thank you.
I read article. But question is still actual. The goto Lable is part of the syntax too. But using of this statement isn’t good I think.
Why using the continue Lable statement (or the break Lable statement) is better than using goto?
Use them when necessary, otherwise not. All of break
, continue
and goto
with a label have good uses.
It is not better, it is different. If you have a use case for continue
or break
, use them. If you don’t have such a use case, don’t use them.
Do you have a specific problem writing code?
I suggest you try to program without labels at all to start with.
I’ve not seen it used much. Like you said, it looks similar to a goto statement. I would treat it like you would treat a goto and use it sparingly.
One typical case where I end up with at least labeled break
is in a switch
. For example:
loop:
for item := range someChan {
switch item.Type {
case typeA:
// something something
case typeB:
// something something, we should stop processing for some reason
break loop
}
}
Without the label, break
breaks the switch
. Similar of course for for
-in-for
but then you can always argue that it could be refactored to avoid so I’m not going to use that as an example.
Backing up Jakob: https://golang.org/doc/effective_go.html#switch
Ah, i see, to break out of the for-loop altogether. Good example.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.