go / beginner
Snippet
Conditional Logic with If-Else
The if statement evaluates a boolean condition. Parentheses are not used around the condition, but curly braces for the block are mandatory.
snippet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package mainimport "fmt"func main() {temp := 25if temp > 30 {fmt.Println("It is hot")} else if temp > 20 {fmt.Println("It is nice")} else {fmt.Println("It is cold")}}
Breakdown
1
if temp > 30 {
Starts the condition check; no parentheses are required in Go.
2
else if temp > 20 {
Checks an alternative condition if the first one was false.
3
} else {
Executes this block if none of the above conditions were met.