Unleashing the Power of Labels in Go: 10 Examples

DSL
4 min readDec 27, 2022

--

Labels are a powerful and often underused feature in Go. They allow you to create loops that can be broken or continued from a specific point within the loop, rather than from the top or bottom. In this post, we’ll explore ten extremely clever ways to use labels in Go. We’ll see how they can be used to create loops that can be exited and re-entered from different points, depending on conditions, variables, function calls, channel values, map values, and slice values. These examples demonstrate the flexibility and expressiveness of labels, and should inspire you to think creatively about how you can use them in your own code.

1: Use labels to create a loop that can be broken or continued from a specific point within the loop, rather than from the top or bottom. For example:

for i := 0; i < 10; i++ {
if i == 5 {
continue loop
}
fmt.Println(i)
}
loop:

2: Use labels to create a loop that can be exited from a specific point within the loop, rather than just at the bottom. For example:

for i := 0; i < 10; i++ {
if i == 5 {
break loop
}
fmt.Println(i)
}
loop:

3: Use labels to create a loop that can be exited from a specific point within the loop, and then immediately re-entered from that same point. For example:

for i := 0; i < 10; i++ {
// fmt.Println(i)
loop:
if i == 5 {
i++
goto loop
}
fmt.Println(i)
}

4: Use labels to create a loop that can be exited from a specific point within the loop, and then immediately re-entered from a different point within the loop. For example:

for i := 0; i < 10; i++ {
if i == 5 {
goto skip
}
fmt.Println(i)
skip:
}

5: Use labels to create a loop that can be exited from a specific point within the loop, and then immediately re-entered from a different point within the loop, depending on a condition. For example:

for i := 0; i < 10; i++ {
if i == 5 {
if someCondition {
goto skip
} else {
goto loop
}
}
fmt.Println(i)
skip:
}
loop:

6: Use labels to create a loop that can be exited from a specific point within the loop, and then immediately re-entered from a different point within the loop, depending on the value of a variable. For example:

for i := 0; i < 10; i++ {
if i == 5 {
switch someVariable {
case 1:
goto skip
case 2:
goto loop
}
}
fmt.Println(i)
skip:
}
loop:

7: Use labels to create a loop that can be exited from a specific point within the loop, and then immediately re-entered from a different point within the loop, depending on the result of a function call. For example:

for i := 0; i < 10; i++ {
if i == 5 {
if someFunction() {
goto skip
} else {
goto loop
}
}
fmt.Println(i)
skip:
}
loop:

8: Use labels to create a loop that can be exited from a specific point within the loop, and then immediately re-entered from a different point within the loop, depending on the value of a channel. For example:

for i := 0; i < 10; i++ {
if i == 5 {
select {
case <-someChannel:
goto skip
default:
goto loop
}
}
fmt.Println(i)
skip:
}
loop:

9: Use labels to create a loop that can be exited from a specific point within the loop, and then immediately re-entered from a different point within the loop, depending on the value of a map. For example:

for i := 0; i < 10; i++ {
if i == 5 {
if _, ok := someMap[i]; ok {
goto skip
} else {
goto loop
}
}
fmt.Println(i)
skip:
}
loop:

10: Use labels to create a loop that can be exited from a specific point within the loop, and then immediately re-entered from a different point within the loop, depending on the value of a map. For example:

2. To make your code more readable, create labels to enclose your loops and exits, rather than relying on the bottom of the loop. For example:

for i := 0; i < 10; i++ {
if i == 5 {
break loop
}
fmt.Println(i)
}
loop:
  1. Use labels to create a loop that can be exited from a specific point within the loop, and then immediately re-entered from that same point. For example:

--

--

DSL
DSL

Written by DSL

Sr software engineer. Love in Go, JavaScript, Python, and serverless AWS. Follow me for tech insights and experiences. follow me on twitter @terraformia

No responses yet