Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created August 16, 2021 20:37
Show Gist options
  • Save percybolmer/a8d1eef158c200128c9ed1cb64566259 to your computer and use it in GitHub Desktop.
Save percybolmer/a8d1eef158c200128c9ed1cb64566259 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, Reader! Your learning about 'goto' statement")
// We create a for loop which runs until i is 10
for i := 0; i < 10; i++ {
// printInteger will try to goto to exit, which will not work
printInteger(i)
}
fmt.Println("Skip this line here")
// Create the exit label and insert code that should be executed when triggered
exit:
fmt.Println("We are now exiting the program")
}
func printInteger(i int) {
fmt.Printf("Index: %d\n", i)
if i == 5 {
// When i is 5, lets exit by using goto
goto exit
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment