Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active August 6, 2024 16:15
Show Gist options
  • Save Integralist/e056ae364b2f10cc01d26d5525ec269b to your computer and use it in GitHub Desktop.
Save Integralist/e056ae364b2f10cc01d26d5525ec269b to your computer and use it in GitHub Desktop.
[Go and the goto statement] #go #golang #goto
// 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