×

# Golang Operators

Go Operators: Learn about the various operators of the Go programming language with examples.
Submitted by IncludeHelp, on December 09, 2021

The operators are the set of symbols to perform specific mathematics and logical operations. The Go programming language has a rich set of operators to perform various mathematical, logical operations, bitwise operations, etc.

## List of Golang Operators

Go language operators are categorized in the following categories –

## 1) Golang Arithmetic Operators

Arithmetic or Mathematical operators are used to perform mathematic operations such as addition, subtraction, multiplication, etc.

List of Golang Arithmetic Operators

Operator Description Example
x:=5
y:=2
Subtraction (-) Subtracts two operands (subtracts the seconds operand from the first operand) x-y returns 3
Multiplication (*) Multiplies two operands x*y returns 10
Division (/) Divides the first operand with the second operand and returns the result x/y returns 2
Modulus (%) Returns the remainder of two operands x%y returns 1
Increment (++) Increases the value of the variable by one x++ returns 6
Decrement (--) Decreases the value of the variable by one x-- returns 4

## 2) Golang Relational Operators

Relational operators are used for making decisions, they are used to compare the expressions such as greater than, less than, equal to, etc. Relational operators return the Boolean value i.e., true or false.

List of Golang Relational Operators

Operator Description Example
x:=5
y:=2
Equal To (==) Returns true if the values of both operands are equal; false, otherwise. x==y returns false
Not Equal To (!=) Returns true if the values of both operands are not equal; true, otherwise. x!=y returns true
Greater Than (>) Returns true if the value of the first operand is greater than the second operator; false, otherwise. x>y returns true
Less Than (<) Returns true if the value of the first operand is less than the second operator; false, otherwise. x<y returns false
Greater Than or Equal To (>=) Returns true if the value of the first operand is greater than or equal to the second operator; false, otherwise. x>=y returns true
Less Than or Equal To (<=) Returns true if the value of the first operand is less than or equal to the second operator; false, otherwise. x<=y returns false

### 3) Golang Logical Operators

Logical operators are used for making decisions based on multiple conditions, they are used to check multiple conditions, i.e., the logical operators are the symbols used to connect two or more expressions/conditions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. The common logical operators are Logical AND (&&), Logical OR (||), and Logical NOT (!).

List of Golang Logical Operators

Operator Description Example
x:=5
y:=2
Logical AND (&&) It returns true if all operands are true; false, otherwise. x==5 && y==2 returns true
Logical OR (||) It returns true if any of the operands is true; false, otherwise. x==5 || y==2 returns true
Logical NOT (!) It returns true if the operand is false; false, otherwise. !(x==5) returns false

## 4) Golang Bitwise Operators

Bitwise operators are used for performing operations on bits, i.e., bitwise operators work on bits and perform the bit-by-bit operation, i.e., the bitwise operators are the operators used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.

List of Golang Bitwise Operators

Operator Description Example
x:=5
y:=3
Bitwise AND (&) It returns 1 in each bit position for which the corresponding bits of both operands are 1s. x & y returns 1
Bitwise OR (|) It returns 1 in each bit position for which the corresponding bits of either or both operands are 1s. x | y returns 7
Bitwise XOR (^) It returns 1 in each bit position for which the corresponding bits of either but not both operands are 1s. x ^ y returns 6
Bitwise Left Shift (<<) It shifts the first operand to the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right. x << 2 returns 20
Bitwise Right Shift (>>) It shifts the first operand to the specified number of bits to the right. Excess bits shifted off to the right are discarded. x >> 1 returns 1

## 5) Golang 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

## 6) Golang Miscellaneous Operators

There are a few other special operators which are used in the Golang. Here is the list of some other/miscellaneous operators which are used in the Golang.

List of Golang Miscellaneous Operators

Operator Description Example
x:=5
y:=3
Pointer to a variable (*) Returns the value of a pointer variable. *x
Receive (<-) This operator is used to receive a value from the channel. y := <-ch