Created
June 9, 2016 19:56
-
-
Save eduncan911/a422423f5a42f1825f5ededf9610f1cd to your computer and use it in GitHub Desktop.
Labels in GoLang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
/* | |
Here's a use of labels in Go to continue a for loop | |
from within another for and within a switch case. | |
*/ | |
func main() { | |
foo := []string{"a", "b", "c", "d", "e", "f", "g"} | |
bar := []string{"b", "d", "f"} | |
fooloop: | |
for _, v := range foo { | |
for _, v2 := range bar { | |
switch { | |
case v == v2: | |
continue fooloop | |
} | |
} | |
fmt.Println(v) | |
} | |
} | |
/* | |
output$ go run main.go | |
a | |
c | |
e | |
g | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Real world? Most likely not, you would use
goroutines
instead as it's against best practices when you have more than 10 lines of code in any one function.However, most low-level code is written in this matter (MOV,ATTR). I've seen converters that takes assembly and converts to C and other higher-level languages, filled with goto labels.
GOTO labels do have a place in complex logic functions where you would have a single-escape vector instead of dozens of
return
statements (with common logic).Say for example:
I would not want to repeat all of those return logic statements, in each switch statement. The goto/label branches makes things DRY and cleans up the code. Personally, I don't write code this way in Golang. But, i have been known to handle quite complex logic in C++/C#/Java this way in the past.
GOTO/labels are not all that bad. It's the implementer/developer that may misuse them. There is a right way to use GOTO labels, and many many wrong ways.