Conditionals
Go's if statement drops the parentheses around the condition that C-family languages require, but makes up for it by requiring curly braces every single time, no exceptions.
if, else if, else
package main import "fmt" func main() { score := 82 if score >= 90 { fmt.Println("A") } else if score >= 80 { fmt.Println("B") } else { fmt.Println("C or below") } }
B
Go checks each condition top to bottom and runs the first branch that's true — score >= 90 fails, score >= 80 succeeds, so "B" prints and the else never runs.
{ must be on the same line as if, else if, or else — Go's compiler actually enforces this (it's not just a style guide rule), because Go automatically inserts semicolons at the end of lines under certain rules, and a brace on its own line would break that. gofmt, Go's built-in formatter, will fix this for you automatically if you run it.if with an init statement
Go lets you run a short statement right before the condition, scoped only to the if/else block — handy for a value you only need for the check itself:
package main import "fmt" func half(n int) int { return n / 2 } func main() { if h := half(9); h > 3 { fmt.Println("half is more than 3:", h) } else { fmt.Println("half is 3 or less:", h) } }
half is more than 3: 4
h := half(9) runs first, then h > 3 is checked — and h stays visible and usable in both the if and else branches, but doesn't exist anywhere outside this statement. This keeps helper values from leaking into the rest of the function when they're only needed for one check.
switch
Go's switch doesn't fall through to the next case by default, unlike C, C++, Java, or JavaScript — each case stops on its own, so you don't need a break:
package main import "fmt" func main() { day := "Tue" switch day { case "Sat", "Sun": fmt.Println("Weekend") case "Mon", "Tue", "Wed", "Thu", "Fri": fmt.Println("Weekday") default: fmt.Println("Not a day") } }
Weekday
A single case can list several values separated by commas, as shown here, which covers a lot of what other languages need multiple fall-through cases for.