What comparison operators do
A comparison operator looks at two values and produces a bool: true or false. That boolean is exactly what an if statement needs as its condition. Go has six of them:
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
Here is how each one answers for three grades compared with 5:
| grade | < 5 | <= 5 | == 5 | != 5 | > 5 | >= 5 |
|---|---|---|---|---|---|---|
| 4 | true | true | false | true | false | false |
| 5 | false | true | true | false | false | true |
| 8 | false | false | false | true | true | true |
The row for 5 is the one that trips people up: < and > exclude the boundary, while <= and >= include it.
Using a comparison in an if
grade := 8
if grade < 5 {
fmt.Println("Failed the test")
} else {
fmt.Println("Passed the test")
}
The comparison grade < 5 is false for 8, so the else block runs and the program prints Passed the test. You can also keep the result in a variable: ok := grade >= 5 gives ok the type bool.
Comparing variables and strings
Either side of a comparison can be a variable, a literal or a more complex expression. A named threshold is often clearer than a bare number:
grade := 8
passMark := 5
fmt.Println(grade >= passMark)
passed := grade >= passMark
fmt.Printf("%T %v\n", passed, passed)
This prints true and then bool true. If the pass mark ever changes, only one line of the program needs an edit. The same operators work on strings, so if name == "Ana" { is the normal way to test text. Use == for this; Go has no separate "equals" method for strings.
Go-specific rules
Both sides must have the same type
Go never converts numbers automatically, so comparing an int with a float64 is a compile error:
var i int = 3
var f float64 = 3.5
fmt.Println(i < f)
The compiler reports invalid operation: i < f (mismatched types int and float64). Convert one side explicitly: float64(i) < f. A plain number written in the code, such as the 5 in grade < 5, adapts to the other operand's type, so no conversion is needed there.
== compares values, not just numbers
Strings compare by their contents, and the ordering operators compare them byte by byte, which for plain English text means alphabetical order with all capital letters before lowercase ones:
fmt.Println("apple" < "banana", "Zebra" < "apple", "go" == "Go")
This prints true true false. Arrays of the same type can be compared with == too, element by element. Slices and maps can't: they can only be compared with nil. For example, a slice declared as var s []int has no elements and no backing array yet, and s == nil is true. Comparing two slices with each other is a compile error, so to check that they hold the same elements you compare them element by element in a loop.
No chained comparisons
In mathematics you write 1 < x < 5. Go reads it as (1 < x) < 5, which compares a boolean with a number, and the compiler refuses it. Join two separate comparisons with && instead: 1 < x && x < 5.
Floating-point numbers
Fractions such as 0.1 and 0.2 can't be stored exactly in binary. With x, y := 0.1, 0.2, the expression x+y == 0.3 is false, because x+y prints as 0.30000000000000004. For decimals, compare with a small tolerance rather than ==. Integers have no such problem.
The exercise
The lesson gives grade := 8 and asks you to decide whether it is a passing grade. A grade below 5 fails; 5 or more passes.
- Write
if grade < 5 {. Strictly smaller is right, because a 5 is a pass. - Print
"Failed the test"inside the block withfmt.Println. - Close the block with
} else {on the same line, print"Passed the test"and close with}.
grade := 8
if grade < 5 {
fmt.Println("Failed the test")
} else {
fmt.Println("Passed the test")
}
Try changing grade to 5 and to 4 in the editor above. Only 4 should take the first branch.
Common mistakes
- Writing
=where==is meant.=assigns. Go doesn't accept an assignment as a condition and reportscannot use assignment x = 3 as value. - Getting the boundary wrong.
grade <= 5would fail a grade of 5. - Comparing mixed numeric types without a conversion.
- Chaining comparisons like
1 < x < 5. - Using
==on floating-point results and expecting exact equality.
Key takeaways
- The six operators produce a
bool, ready to use in anif. - Know which operators include the boundary value and which don't.
- Both operands must have the same type; convert explicitly when they don't.
- Combine tests with
&&; Go has no chained comparisons.