×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Assignment Operators

Last Updated : April 19, 2025

Assignment operators are used for assigning the expressions or values to the variable/ constant etc. These operators assign the result of the right-side expression to the left-side variable or constant. The "=" is an assignment operator. Assignment operators can also be the combinations of some other operators (+, -, *, /, %, etc.) and "=". Such operators are known as compound assignment operators.

List of Assignment Operators

Operator Description Example
x:=5
y:=3
Assignment (=) It assigns the result of the expression written on the right side to the variable written on the left side. x = 10 or x = y+10 or x = x+5, etc.
Add and Assignment (+=) It is the combination of '+' and '=' operators, it adds the given value with the current value of the variable and assigns it to the variable. x +=y is equivalent to x = x+y
Subtract and Assignment (-=) It is the combination of '-' and '=' operators, it subtracts the given value with the current value of the variable and assigns it to the variable. x -=y is equivalent to x = x-y
Multiply and Assignment (*=) It is the combination of '*' and '=' operators, it multiplies the given value with the current value of the variable and assigns it to the variable. x *=y is equivalent to x = x*y
Divide and Assignment (/=) It is the combination of '/' and '=' operators, it divides the given value with the current value of the variable and assigns it to the variable. x /=y is equivalent to x = x/y
Modulus and Assignment (%=) It is the combination of '%' and '=' operators, it finds the remainder of the current value of the variable with the given value and assigns it to the variable. x %=y is equivalent to x = x%y
Bitwise AND Assignment (&=) It is the combination of '&' and '=' operators, it performs the Bitwise AND operation with the current value of the variable and the given value and assigns the result to the variable. x &=y is equivalent to x = x&y
Bitwise OR Assignment (|=) It is the combination of '|' and '=' operators, it performs the Bitwise OR operation with the current value of the variable and the given value and assigns the result to the variable. x |=y is equivalent to x = x|y
Bitwise XOR Assignment (^=) It is the combination of '^' and '=' operators, it performs the Bitwise XOR operation with the current value of the variable and the given value and assigns the result to the variable. x ^=y is equivalent to x = x^y
Bitwise Left Shift Assignment (<<=) It is the combination of '<<' and '=' operators, it performs the Bitwise Left-shift operation with the current value of the variable and the given value and assigns the result to the variable. x <<=y is equivalent to x = x<<y
Bitwise Right Shift Assignment (>>=) It is the combination of '>>' and '=' operators, it performs the Bitwise Left-shift operation with the current value of the variable and the given value and assigns the result to the variable. x >>=y is equivalent to x = x>>y

Example of Assignment Operators

The below Golang program is demonstrating the example of assignment operators.

// Golang program demonstrate the
// example of assignment operators

package main

import "fmt"

func main() {
	x := 5
	y := 3

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

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

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

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

	x %= y
	fmt.Println("x:", x)

	x &= y
	fmt.Println("x:", x)

	x |= y
	fmt.Println("x:", x)

	x <<= y
	fmt.Println("x:", x)

	x >>= y
	fmt.Println("x:", x)
}

Output:

x: 8
x: 5
x: 15
x: 5
x: 2
x: 2
x: 3
x: 24
x: 3

Go Assignment Operators Exercise

Select the correct option to complete each statement about assignment operators in Go.

  1. The operator ___ is used to assign a value to a variable in Go.
  2. The operator ___ adds a value to the existing variable in Go.
  3. The operator ___ subtracts a value from the existing variable in Go.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.