if / else
Try it yourself →Executes code based on a condition.
Syntax
if condition { } else { }Description
Condition WITHOUT PARENTHESES, blocks with curly braces. Intermediate conditions are added with else if.
Example
package main
import "fmt"
func main() {
score := 75
if score >= 50 {
fmt.Println("Passed")
} else {
fmt.Println("Failed")
}
}Expected output
Passed
When to use?
Flow branching.
Common mistakes
- ⚠️ Do NOT put the condition in parentheses; { } are mandatory.