Home »
Golang
Golang Relational Operators
Last Updated : April 19, 2025
Relational operators are used for making decisions, they are used to compare the expressions such as greater than, less than, equal to, etc. Relational operators return the Boolean value i.e., true or false.
List of Relational Operators
Operator |
Description |
Example x:=5 y:=2 |
Equal To (==) |
Returns true if the values of both operands are equal; false, otherwise. |
x==y returns false |
Not Equal To (!=) |
Returns true if the values of both operands are not equal; true, otherwise. |
x!=y returns true |
Greater Than (>) |
Returns true if the value of the first operand is greater than the second operator; false, otherwise. |
x>y returns true |
Less Than (<) |
Returns true if the value of the first operand is less than the second operator; false, otherwise. |
x<y returns false |
Greater Than or Equal To (>=) |
Returns true if the value of the first operand is greater than or equal to the second operator; false, otherwise. |
x>=y returns true |
Less Than or Equal To (<=) |
Returns true if the value of the first operand is less than or equal to the second operator; false, otherwise. |
x<=y returns false |
Example of Relational Operators
The below Golang program is demonstrating the example of relational operators.
// Golang program demonstrate the
// example of relational operators
package main
import "fmt"
func main() {
x := 5
y := 2
var result bool
result = x == y
fmt.Println(x, "==", y, "=", result)
result = x != y
fmt.Println(x, "!=", y, "=", result)
result = x < y
fmt.Println(x, "<", y, "=", result)
result = x > y
fmt.Println(x, ">", y, "=", result)
result = x >= y
fmt.Println(x, ">=", y, "=", result)
result = x <= y
fmt.Println(x, "<=", y, "=", result)
}
Output:
5 == 2 = false
5 != 2 = true
5 < 2 = false
5 > 2 = true
5 >= 2 = true
5 <= 2 = false
Go Relational Operators Exercise
Select the correct option to complete each statement about relational operators in Go.
- The operator ___ is used to check if two values are equal in Go.
- In Go, the operator ___ is used to check if one value is greater than the other.
- The operator ___ is used to check if two values are not equal in Go.
Advertisement
Advertisement