goto statements
suggest changeA goto
statement transfers control to the statement with the corresponding label within the same function.
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)
5 is odd
18 is even
goto
can't skip over variable declarations:
goto end
a := 3
fmt.Printf("a: %d\n", a)
end:
# test
./main.go:9:7: goto end jumps over declaration of a at ./main.go:10:4
exit status 2
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents