If Statements
Go’s conditional syntax is similar to other C-style languages but with a few distinct differences designed for readability and safety.
Basic Syntax
In Go, you do not use parentheses around the condition.
if height > 4 {
fmt.Println("You are tall enough!")
}
Else and Else If
You can chain multiple conditions using else if and provide a fallback with else.
if height > 6 {
fmt.Println("You are super tall!")
} else if height > 4 {
fmt.Println("You are tall enough!")
} else {
fmt.Println("You are not tall enough!")
}
The “Brace” Rule
Unlike some languages where brace placement is a matter of style, Go is strict: the opening brace { must be on the same line as the if or else statement.
// CORRECT
if condition {
}
// WRONG - This will fail to compile
if condition
{
}