×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Relational Operators

By IncludeHelp Last updated : October 05, 2024

Golang Relational Operators

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 Golang 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 Golang 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



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.