Go Assignment Operators

Introduction

Assignment operators in Go are used to assign values to variables. These operators not only assign values but can also perform arithmetic or bitwise operations before assigning the result to the variable. Understanding these operators is essential for writing concise and efficient code. In this chapter, you will learn the different assignment operators in Go, with examples for each type.

Assignment Operators in Go

Simple Assignment (=)

The simple assignment operator assigns the value of the right operand to the left operand.

Syntax:

variable = value

Example:

package main

import "fmt"

func main() {
    var a int
    a = 5
    fmt.Println("a:", a) // Output: a: 5
}

Add and Assign (+=)

The add and assign operator adds the right operand to the left operand and assigns the result to the left operand.

Syntax:

variable += value

Example:

package main

import "fmt"

func main() {
    var a int = 5
    a += 3
    fmt.Println("a:", a) // Output: a: 8
}

Subtract and Assign (-=)

The subtract and assign operator subtracts the right operand from the left operand and assigns the result to the left operand.

Syntax:

variable -= value

Example:

package main

import "fmt"

func main() {
    var a int = 5
    a -= 3
    fmt.Println("a:", a) // Output: a: 2
}

Multiply and Assign (*=)

The multiply and assign operator multiplies the left operand by the right operand and assigns the result to the left operand.

Syntax:

variable *= value

Example:

package main

import "fmt"

func main() {
    var a int = 5
    a *= 3
    fmt.Println("a:", a) // Output: a: 15
}

Divide and Assign (/=)

The divide and assign operator divides the left operand by the right operand and assigns the result to the left operand.

Syntax:

variable /= value

Example:

package main

import "fmt"

func main() {
    var a int = 6
    a /= 3
    fmt.Println("a:", a) // Output: a: 2
}

Modulus and Assign (%=)

The modulus and assign operator takes the modulus using two operands and assigns the result to the left operand.

Syntax:

variable %= value

Example:

package main

import "fmt"

func main() {
    var a int = 5
    a %= 3
    fmt.Println("a:", a) // Output: a: 2
}

Bitwise AND and Assign (&=)

The bitwise AND and assign operator performs a bitwise AND operation between the left operand and the right operand and assigns the result to the left operand.

Syntax:

variable &= value

Example:

package main

import "fmt"

func main() {
    var a int = 5  // 0101 in binary
    a &= 3         // 0011 in binary
    fmt.Println("a:", a) // Output: a: 1 (0001 in binary)
}

Bitwise OR and Assign (|=)

The bitwise OR and assign operator performs a bitwise OR operation between the left operand and the right operand and assigns the result to the left operand.

Syntax:

variable |= value

Example:

package main

import "fmt"

func main() {
    var a int = 5  // 0101 in binary
    a |= 3         // 0011 in binary
    fmt.Println("a:", a) // Output: a: 7 (0111 in binary)
}

Bitwise XOR and Assign (^=)

The bitwise XOR and assign operator performs a bitwise XOR operation between the left operand and the right operand and assigns the result to the left operand.

Syntax:

variable ^= value

Example:

package main

import "fmt"

func main() {
    var a int = 5  // 0101 in binary
    a ^= 3         // 0011 in binary
    fmt.Println("a:", a) // Output: a: 6 (0110 in binary)
}

Bitwise AND NOT and Assign (&^=)

The bitwise AND NOT and assign operator performs a bitwise AND NOT operation between the left operand and the right operand and assigns the result to the left operand.

Syntax:

variable &^= value

Example:

package main

import "fmt"

func main() {
    var a int = 5  // 0101 in binary
    a &^= 3        // 0011 in binary
    fmt.Println("a:", a) // Output: a: 4 (0100 in binary)
}

Left Shift and Assign (<<=)

The left shift and assign operator shifts the bits of the left operand to the left by the number of positions specified by the right operand and assigns the result to the left operand.

Syntax:

variable <<= value

Example:

package main

import "fmt"

func main() {
    var a int = 5  // 0101 in binary
    a <<= 1        // shift left by 1
    fmt.Println("a:", a) // Output: a: 10 (1010 in binary)
}

Right Shift and Assign (>>=)

The right shift and assign operator shifts the bits of the left operand to the right by the number of positions specified by the right operand and assigns the result to the left operand.

Syntax:

variable >>= value

Example:

package main

import "fmt"

func main() {
    var a int = 5  // 0101 in binary
    a >>= 1        // shift right by 1
    fmt.Println("a:", a) // Output: a: 2 (0010 in binary)
}

Conclusion

Assignment operators in Go are used to modify and assign values to variables. These operators help streamline your code by combining operations and assignments into single statements.

Leave a Comment

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

Scroll to Top