if Statements and Init Expressions
Conditionals with optional init statements
if in Go
Go's if statement evaluates a boolean expression — no parentheses required, but braces are mandatory:
if x > 0 {
// positive
}else and else if
Chain conditions with else if and handle the fallback with else:
func classify(n int) string {
if n < 0 {
return "negative"
} else if n == 0 {
return "zero"
} else {
return "positive"
}
}All lessons in this course
- if Statements and Init Expressions
- The for Loop: All Patterns
- switch Statements in Go
- break, continue and Labels