Published in


Labels in Go

Label is used in break and continue statement where it’s optional but it’s required in goto statement. Scope of the label is the function where it’s declared. It doesn’t start after the declaration though as with (short) variable declaration but is available for the whole function body:

(notice that label is declared after it’s used in goto statement)

Scope doesn’t contain nested functions so it’s illegal to write:

as it gives “label End not defined” error. This code unveils another compilation-time issue “label End defined and not used” which is a consequence of the requirement that every declared label has to be used.

Labels are not block scoped (more about blocks and scoping in “Blocks in Go” and “Scopes in Go”) so it’s impossible to redeclare label inside nested block:

as Go compiler will complain about already declared label.

Identifiers of the labels live in a separate space so they don’t conflict with i.e. variables identifiers. The code below works just fine even that x is used for two things:

break statement

break statement is traditionally used to terminate innermost for or switch statement. In Go it’s also possible to end this way novel select statement.

People having experience with languages like C and derivates know that each case clause needs to have break as a last statement. Otherwise execution in transferred to the next case clause which is not what programmer wants most of the time. Go however works conversely — there is a fallthrough statement to explicitly move to the next clause:

fallthrough can be used only as a last statement inside case clause.

break statement cannot span over function boundaries:

as it gives compilation error “break is not in a loop”.

Normally break without optional label terminates innermost for, switch or select statement. Adding label gives more possibilities…

Label for break statement must be the one associated with enclosing for, switch or select statement. It’s impossible to compile:

as it throws “invalid break label FirstLoop” error message since label is associated with not enclosing for loop.

It’s valid to terminate other outer (enclosing) loops:

If break statement is placed inside f.ex. loop it doesn’t mean it can only terminate loop:

It’s possible to terminate for, switch or select no matter where break statement is located.

continue statement

It works in a similar way to break statement but begins next iteration instead of stopping and can be used only for loops:

goto statement

It’s used to move execution to the labeled statement:

(notice that “End” labels empty statement which is also permitted)

In contrast to break or continue statements, label is required.

goto can move control only within the same function. Since it’s possible to transfer execution forward there are additional two rules:

  • any variable declaration cannot be skipped so causing a variable to come into scope if it wasn’t at the point where goto is used:

Compiler will detect it as an error “goto Done jumps over declaration of v at…”.

  • goto cannot move into other block :

as it causes “goto Block jumps into block starting at …” error.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michał Łowicki

Software engineer at Datadog, previously at Facebook and Opera, never satisfied.