Last active
August 6, 2024 16:15
-
-
Save Integralist/e056ae364b2f10cc01d26d5525ec269b to your computer and use it in GitHub Desktop.
[Go and the goto statement] #go #golang #goto
This file contains 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
// https://play.golang.com/p/EAtzwpDeb30 | |
// https://go.dev/ref/spec#Goto_statements | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
goTo("beep") | |
goTo("boop") | |
goTo("xxxx") | |
} | |
func goTo(location string) { | |
switch location { | |
case "beep": | |
goto beep | |
case "boop": | |
goto boop | |
default: | |
fmt.Println("unrecognised location") | |
return | |
} | |
beep: | |
fmt.Println("beep was reached") | |
return // IMPORTANT: Go will continue to execute the following code, so return to avoid accidentally printing "boop was reached" | |
boop: | |
fmt.Println("boop was reached") | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment