Go Operators


Go Operators

In Go, operators are special symbols used to perform operations on variables and values.They are essential for computations, logical decisions, and manipulations in Go programs. Go provides several categories of operators, including arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators.


1. Arithmetic Operators

Arithmetic operators are used for fundamental mathematical operations like addition, subtraction, multiplication, and division.

Syntax:

result := operand1 operator operand2

Example:

package main

import "fmt"

func main() {
    a, b := 10, 5
    
    fmt.Println("Addition:", a + b)  
    fmt.Println("Subtraction:", a - b)  
    fmt.Println("Multiplication:", a * b)  
    fmt.Println("Division:", a / b)  
    fmt.Println("Modulus:", a % b)  
}

Output:

Addition: 15  
Subtraction: 5  
Multiplication: 50  
Division: 2  
Modulus: 0 

2. Relational (Comparison) Operators

These operators compare two values and return a Boolean result of either true or false.

Syntax:

result := operand1 operator operand2

Example:

package main

import "fmt"

func main() {
    x, y := 10, 20

    fmt.Println("Equal:", x == y)  
    fmt.Println("Not Equal:", x != y)  
    fmt.Println("Greater than:", x > y)  
    fmt.Println("Less than:", x < y)  
    fmt.Println("Greater than or equal:", x >= y)  
    fmt.Println("Less than or equal:", x <= y)  
}

Output:

Equal: false  
Not Equal: true  
Greater than: false  
Less than: true  
Greater than or equal: false  
Less than or equal: true

3. Logical Operators

Logical operators are used for boolean expressions and decision-making.

Syntax:

result := condition1 operator condition2

Example:

package main

import "fmt"

func main() {
    a, b := true, false

    fmt.Println("AND:", a && b)  
    fmt.Println("OR:", a || b)  
    fmt.Println("NOT a:", !a)  
}

Output:

AND: false  
OR: true  
NOT a: false  

4. Bitwise Operators

Bitwise operators perform operations on the binary representation of numbers.

Syntax:

result := operand1 operator operand2

Example:

package main

import "fmt"

func main() {
    a, b := 5, 3  

    fmt.Println("Bitwise AND:", a & b)  
    fmt.Println("Bitwise OR:", a | b)  
    fmt.Println("Bitwise XOR:", a ^ b)  
    fmt.Println("Left Shift:", a << 1)  
    fmt.Println("Right Shift:", a >> 1)  
}

Output:

Bitwise AND: 1  
Bitwise OR: 7  
Bitwise XOR: 6  
Left Shift: 10  
Right Shift: 2  

5. Assignment Operators

Assignment operators assign values to variables.

Syntax:

variable operator value

Example:

package main

import "fmt"

func main() {
    x := 10  

    x += 5  
    fmt.Println("x += 5:", x)  

    x -= 3  
    fmt.Println("x -= 3:", x)  

    x *= 2  
    fmt.Println("x *= 2:", x)  

    x /= 4  
    fmt.Println("x /= 4:", x)  

    x %= 3  
    fmt.Println("x %= 3:", x)  
}

Output:

x += 5: 15  
x -= 3: 12  
x *= 2: 24  
x /= 4: 6  
x %= 3: 0  

6. Miscellaneous Operators

Increment (++) and Decrement (--) Operators

These operators increment or decrement a variable’s value by one.

Example:

package main

import "fmt"

func main() {
    x := 5  

    x++  
    fmt.Println("Increment:", x)  

    x--  
    fmt.Println("Decrement:", x)  
}

Output:

Increment: 6  
Decrement: 5  

Conclusion

Go offers a variety of operators for arithmetic, logical, comparison, bitwise, and assignment operations. These operators help developers manipulate data efficiently and implement logic in programs. Understanding them is crucial for writing effective Go code.

PreviousNext