Kacper Wikieł fbf17474ce
Add clarification for else syntax
Rationale: https://github.com/golang/go/issues/5440

else has to be in same line as closing parenthesis. Otherwise people coming from C/C++ would see not so obvious error
2019-10-03 19:40:43 +02:00

38 lines
838 B
Go

// Branching with `if` and `else` in Go is
// straight-forward.
package main
import "fmt"
func main() {
// Here's a basic example.
if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}
// You can have an `if` statement without an else.
if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}
// A statement can precede conditionals; any variables
// declared in this statement are available in all
// branches.
if num := 9; num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
// else has to be in same line as closing parenthesis
// Moving else to the next line would cause a syntax error
}
// Note that you don't need parentheses around conditions
// in Go, but that the braces are required.