Can you provide an example of using relational operators in Go?

Certainly! In Go, relational operators are used to compare two values. The common relational operators include:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

Here’s an example demonstrating the use of relational operators in Go:

package main

import "fmt"

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

    // Using relational operators
    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
}

Output:

a == b: false
a != b: true
a > b: false
a < b: true
a >= b: false
a <= b: true

In this example, we compare two integer variables a and b using various relational operators, and the results are printed to the console. Each comparison evaluates to a boolean value (true or false).

0 Comments

no data
Be the first to share your comment!