I'm suggesting that there might be a way of implicitly labeling the nearest
loop. For example:
for {
switch rand.IntN(3) {
case 0:
case 1:
break for
case 2:
}
}
At the moment, you have to explicitly define the label:
func main() {
myloop:
for {
switch rand.IntN(3) {
case 0:
case 1:
break myloop
case 2:
}
}
}
This works and there's nothing wrong with it, but maybe there's a case for
the implicit form too.
I recognise that there are yet other ways of dealing with the problem, but
the 'implicit label' seems like a neat solution to me.
On Saturday, 20 September 2025 at 19:08:03 UTC+1 Ian Lance Taylor wrote:
> On Sat, Sep 20, 2025, 8:58 AM Stephen Illingworth <[email protected]>
> wrote:
>
>> On Saturday, 20 September 2025 at 16:43:39 UTC+1 Ian Lance Taylor wrote:
>>
>>
>> My recollection is that we discussed how to handle an unlabeled break
>> statement in a select statement. Should it break out of the select or
>> should it break out of the enclosing loop? We wanted break in a switch to
>> break out of the switch, not the loop, to be less confusing to C
>> programmers. And select and switch look pretty similar on the page.
>> Handling break the same way for both seemed to make the most sense.
>>
>>
>> It would be nice if break and continue could have an optional "argument"
>> to indicate what to break out of. For example, to break out of a loop
>> inside a switch block you could write "break loop". It would be an implicit
>> label on the nearest loop.
>>
>> I feel this might be common enough to be useful but I can't prove that.
>> At the moment I wouldn't bother with labelling at all and I'm more likely
>> to put the loop inside an anonymous function and return from within the
>> switch.
>>
>
> Unless I misunderstand, Go already has that feature.
>
> Ian
>
>>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/f10f245f-8120-41aa-ad29-b11ab1b8e890n%40googlegroups.com.