goto statements

suggest change

A goto statement transfers control to the statement with the corresponding label within the same function.

main.go

func printIsOdd(n int) {
	if n%2 == 1 {
		goto isOdd
	}
	fmt.Printf("%d is even\n", n)
	return

isOdd:
	fmt.Printf("%d is odd\n", n)
}

	printIsOdd(5)
	printIsOdd(18)

Output:

5 is odd
18 is even

goto can't skip over variable declarations:

main.go

	goto end
	a := 3
	fmt.Printf("a: %d\n", a)
end:

Output:

# test
./main.go:9:7: goto end jumps over declaration of a at ./main.go:10:4
exit status 2

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents