Go Conditionals
Go handles decision-making through if/else and switch statements.
1. If/Else Statements
In Go, you don't need parentheses () around the condition, but curly braces {} are required.
2. If with Initializer
A unique feature of Go is that if statements can start with a short statement to execute before the condition. Variables declared here are only available inside the if block.
3. Switch Statements
switch is a cleaner way to write multiple if chains.
4. Switch without Expression
A switch without an expression is an alternate way to write long if/else if chains.
Key Differences from other languages
- No implicit fallthrough: Go automatically breaks after each case. You don't need to write
break. fallthroughkeyword: If you want the execution to continue to the next case, you must explicitly use thefallthroughkeyword.- Multiple values: You can use commas to separate multiple values in a single
case:case "Saturday", "Sunday":.