go / beginner
Snippet
If Statement with Comparison Operators
If statements in Go evaluate boolean conditions and execute code blocks accordingly. Unlike many languages, Go does not use parentheses around conditions. The else if chain allows for multiple conditions. Comparison operators include == (equal), != (not equal), >= (greater or equal), <= (less or equal), > (greater), < (less), and logical operators && (AND), || (OR), ! (NOT).
snippet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package mainimport "fmt"func main() {age := 18if age >= 18 {fmt.Println("Access granted - you are an adult")} else {fmt.Println("Access denied - you are a minor")}score := 85if score >= 90 {fmt.Println("Grade: A")} else if score >= 80 {fmt.Println("Grade: B")} else if score >= 70 {fmt.Println("Grade: C")} else {fmt.Println("Grade: F")}name := ""if len(name) > 0 && name != "" {fmt.Println("Name provided:", name)} else {fmt.Println("No name provided")}}
Breakdown
1
if age >= 18 { }
Simple if condition - no parentheses needed around the boolean expression
2
else { }
Else block executes when the if condition is false
3
else if score >= 80 { }
Chained else if allows testing multiple sequential conditions
4
&& (AND)
Logical AND operator - both conditions must be true
5
|| (OR)
Logical OR operator - at least one condition must be true