ADVERTISEMENT
ADVERTISEMENT

Golang Assignment Operators

Here, we are going to learn about the Assignment Operators in the Go programming language with examples.
Submitted by IncludeHelp, on December 08, 2021

Assignment Operators

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 Golang 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 Golang 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

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.