Go Relational Operators – Compare Values in Go with Examples (2025 Guide)
Introduction – What Are Relational Operators in Go?
Relational operators in Go are used to compare two values. The result of a relational operation is always a boolean (true or false). These comparisons are fundamental in decision-making, such as in if statements, loops, or filtering logic.
In this section, you’ll learn:
- All relational operators available in Go
- Syntax with examples and outputs
- Comparison rules for different data types
- Real-world use cases and edge cases
List of Relational Operators in Go
| Operator | Description | Example | Output |
|---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
> | Greater than | 7 > 5 | true |
< | Less than | 3 < 9 | true |
>= | Greater than or equal to | 6 >= 6 | true |
<= | Less than or equal to | 4 <= 5 | true |
These work for numbers, strings, and booleans (with some constraints).
Syntax – Using Relational Operators
package main
import "fmt"
func main() {
a, b := 10, 20
fmt.Println(a == b) // false
fmt.Println(a != b) // true
fmt.Println(a < b) // true
fmt.Println(a > b) // false
fmt.Println(a <= b) // true
fmt.Println(a >= b) // false
}
Output:
false
true
true
false
true
false
String Comparisons in Go
Go compares strings lexicographically (dictionary order based on Unicode values).
fmt.Println("apple" < "banana") // true
fmt.Println("Go" == "Go") // true
Go is case-sensitive:
"Go" != "go"
Boolean Comparisons
You can compare boolean values only with == or !=.
var a, b bool = true, false
fmt.Println(a == b) // false
fmt.Println(a != b) // true
You cannot use
<,>,<=,>=with booleans — it will cause a compile-time error.
Real-World Example – Age Checker
package main
import "fmt"
func main() {
age := 17
if age >= 18 {
fmt.Println("Eligible to vote.")
} else {
fmt.Println("Not eligible to vote.")
}
}
Output:
Not eligible to vote.
Common Mistakes
| Mistake | Fix |
|---|---|
Mixing different types (e.g. int and string) | Ensure both operands are same type |
Using > on booleans | Use only == or != for bools |
Using assignment = instead of equality == | Double-check operators in conditions |
Summary – Recap & Next Steps
Relational operators in Go help control program flow through comparison logic. Use them in conditionals, loops, filters, and validation logic.
Key Takeaways:
- Use
==,!=,>,<,>=,<=for comparisons - Strings are compared lexicographically
- Booleans support only
==and!= - Always ensure operand types match for valid comparison
Next: Learn about Go Logical Operators (&&, ||, !) to combine multiple relational expressions.
FAQs – Relational Operators in Go
Can I compare different data types like int and string in Go?
No. Go requires both operands to be of the same type. You must convert explicitly.
Are relational operators allowed on structs or arrays?
Only == and != are allowed on comparable types (not slices, maps, or functions). Others like > are not valid for structs.
How are strings compared in Go?
Strings are compared by Unicode value, character by character.
Can I use relational operators on booleans?
Only == and != are valid for booleans. <, >, etc., will cause an error.
What does x <= y mean in Go?
It checks if x is less than or equal to y. It returns a boolean result.
Share Now :
