if / else Statements
if condition {
// body
} else if condition2 {
// body
} else {
// body
}
if init; condition {
// body
}
Basic if / else
An if statement runs a block only when its condition evaluates to true, and an optional else block runs otherwise. Unlike C or Java, the condition is never wrapped in parentheses, and the braces are always required, even for a single statement.
Example: Basic if / else
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create age and infer the type
age := 20
// Check whether age >= 18
if age >= 18 {
// Print the values followed by a newline
fmt.Println("adult")
} else {
// Print the values followed by a newline
fmt.Println("minor")
}
}
Login to try C/C++/Java/PHP code in the editor
else if Chains
Multiple conditions can be chained with 'else if', evaluated top to bottom until one is true, falling through to a final else if none match -- useful for handling several mutually exclusive cases.
Example: else if Chains
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create score and infer the type
score := 72
// Check whether score >= 90
if score >= 90 {
// Print the values followed by a newline
fmt.Println("Grade A")
} else if score >= 75 {
// Print the values followed by a newline
fmt.Println("Grade B")
} else if score >= 60 {
fmt.Println("Grade C")
} else {
fmt.Println("Grade F")
}
}
Login to try C/C++/Java/PHP code in the editor
if with an Init Statement
Go lets you run a short statement before the condition, separated by a semicolon, most often used to assign a value and immediately test it -- a pattern very common with functions that return a value and an error.
Example: if with an Init Statement
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function half
func half(n int) (int, bool) {
// Check whether n%2 != 0
if n%2 != 0 {
// Send 0, false back to the caller
return 0, false
}
// Send n / 2, true back to the caller
return n / 2, true
}
// main is where the program starts running
func main() {
// Check whether result, ok := half(10); ok
if result, ok := half(10); ok {
fmt.Println("half is:", result)
} else {
fmt.Println("not evenly divisible")
}
}
Login to try C/C++/Java/PHP code in the editor
Boolean Expressions
Conditions combine comparisons with && (and), || (or), and ! (not), just like most C-family languages, and Go short-circuits these operators, stopping evaluation as soon as the result is determined.
Example: Boolean Expressions
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create age and infer the type
age := 25
// Short declaration: create hasID and infer the type
hasID := true
// Check whether age >= 18 && hasID
if age >= 18 && hasID {
// Print the values followed by a newline
fmt.Println("entry allowed")
}
}
Login to try C/C++/Java/PHP code in the editor
- Wrapping the condition in parentheses like 'if (x > 5)' -- Go allows it but idiomatic style always omits them.
- Forgetting that the opening brace must be on the same line as if/else -- Go's automatic semicolon insertion breaks otherwise.
- Not realizing a variable declared in the if's init statement (if x := f(); x > 0) is scoped only to the if/else chain, not outside it.
- if/else branches based on a boolean condition, with no parentheses required around the condition.
- An optional init statement can run before the condition, scoping a variable to the whole if/else chain.
- else if chains multiple conditions together.
- Braces are mandatory in Go, unlike some C-family languages that allow single-statement bodies without them.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: