Go Relational Operators

Introduction

Relational operators are used to compare two values. These comparisons result in a boolean value (true or false). In Go, relational operators help determine the relationship between operands, such as equality, inequality, or ordering. In this chapter, you will learn different relational operators in Go, with examples for each type.

Relational Operators in Go

Equal to (==)

The equal to operator checks if two operands are equal.

Syntax:

result = operand1 == operand2

Example:

package main

import "fmt"

func main() {
    var a int = 5
    var b int = 3
    var isEqual bool = (a == b)
    fmt.Println("a == b:", isEqual) // Output: a == b: false
}

Not equal to (!=)

The not equal to operator checks if two operands are not equal.

Syntax:

result = operand1 != operand2

Example:

package main

import "fmt"

func main() {
    var a int = 5
    var b int = 3
    var isNotEqual bool = (a != b)
    fmt.Println("a != b:", isNotEqual) // Output: a != b: true
}

Greater than (>)

The greater than operator checks if the left operand is greater than the right operand.

Syntax:

result = operand1 > operand2

Example:

package main

import "fmt"

func main() {
    var a int = 5
    var b int = 3
    var isGreater bool = (a > b)
    fmt.Println("a > b:", isGreater) // Output: a > b: true
}

Less than (<)

The less than operator checks if the left operand is less than the right operand.

Syntax:

result = operand1 < operand2

Example:

package main

import "fmt"

func main() {
    var a int = 5
    var b int = 3
    var isLess bool = (a < b)
    fmt.Println("a < b:", isLess) // Output: a < b: false
}

Greater than or equal to (>=)

The greater than or equal to operator checks if the left operand is greater than or equal to the right operand.

Syntax:

result = operand1 >= operand2

Example:

package main

import "fmt"

func main() {
    var a int = 5
    var b int = 5
    var isGreaterOrEqual bool = (a >= b)
    fmt.Println("a >= b:", isGreaterOrEqual) // Output: a >= b: true
}

Less than or equal to (<=)

The less than or equal to operator checks if the left operand is less than or equal to the right operand.

Syntax:

result = operand1 <= operand2

Example:

package main

import "fmt"

func main() {
    var a int = 5
    var b int = 5
    var isLessOrEqual bool = (a <= b)
    fmt.Println("a <= b:", isLessOrEqual) // Output: a <= b: true
}

Combining Relational Operators with Logical Operators

You can combine relational operators with logical operators to form complex conditions.

Example:

package main

import "fmt"

func main() {
    var a int = 5
    var b int = 3
    var c int = 8

    var result bool = (a > b) && (c > a)
    fmt.Println("(a > b) && (c > a):", result) // Output: (a > b) && (c > a): true
}

Conclusion

Relational operators in Go are essential for comparing values and making decisions based on those comparisons. By understanding and using these operators?equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to?you can effectively control the flow of your programs and perform necessary validations.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top