Go – Relational Operators

Relational operators are used to compare two values, resulting in a boolean value: true or false. These operators are commonly used in conditional statements to control the flow of a program.

In this tutorial, we will explore each relational operator in Go with examples and detailed explanations.


List of Relational Operators

The following table lists all relational operators in Go:

OperatorDescriptionExampleResult
==Equal toa == btrue if a is equal to b
!=Not equal toa != btrue if a is not equal to b
>Greater thana > btrue if a is greater than b
<Less thana < btrue if a is less than b
>=Greater than or equal toa >= btrue if a is greater than or equal to b
<=Less than or equal toa <= btrue if a is less than or equal to b

Examples of Relational Operators

Let’s explore how to use relational operators in a Go program.


Example 1: Using Relational Operators with Integers

Here’s a simple program to demonstrate relational operators with integers:

</>
Copy
package main

import "fmt"

func main() {
    a := 10
    b := 20

    fmt.Println("a == b:", a == b) // false
    fmt.Println("a != b:", a != b) // true
    fmt.Println("a > b:", a > b)   // false
    fmt.Println("a < b:", a < b)   // true
    fmt.Println("a >= b:", a >= b) // false
    fmt.Println("a <= b:", a <= b) // true
}

Explanation

  1. Declare Variables: Two integer variables, a and b, are initialized with the values 10 and 20, respectively.
  2. Use Relational Operators: Each relational operator is applied to compare a and b.
  3. Print Results: The results of the comparisons are printed using fmt.Println.

Output


Example 2: Using Relational Operators with Floating-Point Numbers

Relational operators can also be used with floating-point numbers:

</>
Copy
package main

import "fmt"

func main() {
    x := 15.5
    y := 10.2

    fmt.Println("x == y:", x == y) // false
    fmt.Println("x != y:", x != y) // true
    fmt.Println("x > y:", x > y)   // true
    fmt.Println("x < y:", x < y)   // false
    fmt.Println("x >= y:", x >= y) // true
    fmt.Println("x <= y:", x <= y) // false
}

Explanation

  1. Declare Variables: Two floating-point variables, x and y, are initialized with the values 15.5 and 10.2, respectively.
  2. Use Relational Operators: Each relational operator is applied to compare x and y.
  3. Print Results: The results of the comparisons are printed using fmt.Println.

Output


Example 3: Using Relational Operators with Strings

Relational operators can also compare strings lexicographically (dictionary order):

</>
Copy
package main

import "fmt"

func main() {
    str1 := "Golang"
    str2 := "Go"

    fmt.Println("str1 == str2:", str1 == str2) // false
    fmt.Println("str1 != str2:", str1 != str2) // true
    fmt.Println("str1 > str2:", str1 > str2)   // true
    fmt.Println("str1 < str2:", str1 < str2)   // false
}

Explanation

  1. Declare Variables: Two string variables, str1 and str2, are initialized with the values "Golang" and "Go", respectively.
  2. Use Relational Operators: Relational operators are applied to compare the strings lexicographically.
  3. Print Results: The results of the comparisons are printed using fmt.Println.

Output


Key Notes for Relational Operators

  1. Relational operators return a boolean value: true or false.
  2. They work with integers, floating-point numbers, and strings.
  3. String comparisons are case-sensitive and based on lexicographical order.