Go > Core Go Basics > Control Flow > Labels in flow control
Using Labels to Control Flow in Go
This example demonstrates how to use labels in Go to control the flow of execution within loops and switch statements. Labels provide a way to break out of nested loops or jump to a specific case in a switch based on certain conditions.
Basic Example: Breaking Out of a Nested Loop
This code demonstrates how to use a label (outerLoop
) to break out of a nested for
loop. When the condition i*j == 6
is met, the break outerLoop
statement is executed, which terminates the outer loop immediately. Without the label, only the inner loop would be terminated.
package main
import "fmt"
func main() {
outerLoop:
for i := 0; i < 5; i++ {
for j := 0; j < 5; j++ {
if i*j == 6 {
fmt.Println("Breaking out of outer loop when i=", i, " and j=", j)
break outerLoop // Breaks out of the outer loop
}
fmt.Println("i=", i, "j=", j)
}
}
fmt.Println("Program completed.")
}
Explanation of the Code
outerLoop:
is declared before the outer for
loop. Labels are case-sensitive and must be followed by a colon.break outerLoop
statement within the inner loop tells the program to exit the loop associated with the outerLoop
label.i*j == 6
is true, the program prints a message indicating the values of i
and j
, then breaks out of the outer loop, and execution continues after the outer loop.
Example: Continuing to a Specific Iteration in a Loop
This snippet illustrates how to use a label with the continue
statement. The label loop
is associated with the for
loop. When i
is an even number, the continue loop
statement is executed, which skips the rest of the current iteration and proceeds to the next iteration of the loop.
package main
import "fmt"
func main() {
loop:
for i := 0; i < 10; i++ {
if i%2 == 0 {
fmt.Println("Skipping even number:", i)
continue loop // Skip even numbers
}
fmt.Println("Processing odd number:", i)
}
fmt.Println("Loop finished.")
}
Usage with Switch Statements
This shows how to use a label with a switch
statement. While less common than with loops, a label can be used to break out of a switch
statement. In this example, when num
is 2, the break switchExample
statement exits the entire switch
block.
package main
import "fmt"
func main() {
switchExample: // Label for the switch statement
switch num := 2; {
case 1:
fmt.Println("Number is 1")
case 2:
fmt.Println("Number is 2")
break switchExample // Break out of the switch statement
case 3:
fmt.Println("Number is 3")
default:
fmt.Println("Number is something else")
}
fmt.Println("Switch statement completed.")
}
Concepts Behind Labels in Flow Control
Real-Life Use Case
Imagine searching a two-dimensional array for a specific element. If you find the element, you might want to stop searching immediately and return the coordinates. Labels allow you to efficiently exit the outer loop as soon as the element is found. Another example is handling complex state machines where you want to jump to a particular state based on a series of conditions. Labels provide a way to jump to a specific section of code that handles that state.
Best Practices
goto
statement in the traditional sense. Labels can only be used with break
and continue
statements.
Interview Tip
Be prepared to explain what labels are, how they are used with break
and continue
statements, and why you might choose to use them. Also, be ready to discuss the potential drawbacks of using labels and when they should be avoided.
When to Use Labels
Labels are most beneficial in situations where you need to break out of multiple nested loops or jump to a specific point in a complex control flow. They can improve code clarity in these specific scenarios.
Memory Footprint
Labels themselves do not have a direct memory footprint. They are primarily compile-time constructs used to guide the control flow. The impact on memory usage is negligible.
Alternatives
Alternatives to using labels include refactoring the code to avoid deeply nested loops or using boolean flags to signal when to exit a loop. Sometimes, breaking down a complex function into smaller, more manageable functions can eliminate the need for labels.
Pros
Cons
FAQ
-
Are labels case-sensitive?
Yes, labels in Go are case-sensitive. -
Can I use the same label multiple times in a function?
No, label names must be unique within a function. -
Can labels be used with `goto` statements in Go?
No, Go does not have agoto
statement in the traditional sense. Labels are specifically used withbreak
andcontinue
statements within loops and switch statements.