Operators operate on its operands to return a result or perform some action. Golang provides many different operators which may be categorized under following heads.

Mathematical operators
These operators are used for performing mathematical operations. Golang mathematical operators are described in the below table.

OperatorNameMeaningExample
+AdditionUsed for addition of two or more valuessum := 2 + 5
SubtractionUsed for subtracting one value from anotherdiff := 10 – 2
*MultiplicationMultiplying two or more valuesp := 5 * 2 * 9
/DivisionFor dividing two numbers. It returns the quotient of division.q := 10/5
Result will be 2
%ModulusRemainder of division of two numbers.r := 11/3
Result will be 2
++IncrementFor increasing the value of preceding variable by 1.p := 1
p++
p will be 2
DecrementFor reducing the value of preceding variable by 1.q := 10
q–
q will not be 9
&Bitwise ANDPerforms AND operation on its operands bit by bit.p := 11
q := 21
p & q will be 1
|Bitwise ORPerforms AND operation on its operands bit by bit.p := 10
q := 20
p | q will be 30
<<Left shiftLeft shifts the bits of the operand on the left side by the number of digits on its right side.p := 10
q := p << 3
q will be 80
>>Right shiftRight shifts the bits of the operand on the left side by the number of digits on its right side.p := 10
q := p << 3
q will be 1

Comparison operators
These operators are used for comparing two values to determine if they are equal, not equal or one value is greater or smaller than the other.
Golang supports following comparison operators. All comparison operators return either true or false depending on the values.

OperatorNameMeaningExample
<Less thanFor checking if one value is lesser than other. Will be `false` even if one value is equal to other.p := 10
q := 20
p < q will be `true`
q < p will be `false`
>Greater thanFor checking if one value is greater than other.
Will be `false` even if one value is equal to other.
p := 10
q := 20
p > q will be `false`
q < p will be `true`
<=Less than equal toFor checking if one value is lesser than or equal to other.
Will be `false` even if one value is equal to other.
p := 10
q := 20
r := 10
p < q will be `true`
p <= r will be `true`
>=Greater than equal toFor checking if one value is greater than other.
Will be `false` even if one value is equal to other.
p := 10
q := 20
r := 10
p > q will be `false`
p >= r will be `true`
==Equal toFor checking if both its operands are equal.p := 10
q := 20
r := 20
p == q will be `false`
q == r will be `true`
!=Not equal toFor checking if both its operands are not equal.p := 10
q := 20
r := 20
p != q will be `true`
q != r will be `false`

All comparison operators can only be used among comparable or compatible objects. Example, if you try to compare a string with an integer, then an error will be raised.

invalid operation: r < s (mismatched types int and string)

Logical operators
These operators apply only to boolean values and are used in decision making operations. Following is a list of logical operators in Golang.

OperatorNameMeaningExample
&&Conditional ANDFor performing AND operation between two boolean values.
If any one of the boolean is `false`, the result is `false`.
p := false
q := true
r := true
p && q will be `false`
q && r will be `true`
||Conditional ORFor performing OR operation between two boolean values.
If any one of the boolean is `true`, the result is `true`.
p := false
q := true
r := true
p || q will be `true`
q || r will be `true`
!NOTUsed to reverse a boolean value.p := `false`
!p will be `true`

Assignment operators
Assignment operators assign a value to a variable. Variable is placed at the left hand side while the value is written at the right side.
Value may be a constant or a result of an expression. Assignment operators available in Golang are summarized in table below.

OperatorNameMeaningExample
=AssignmentAssigns value at right side to variable on left.var p int
p = 2
:=Declaration operatorDeclares a variable and assigns value to it. With this operator, a variable is declared and initialized in the same statement.p := 2
+=Addition assignment operatorAdds the value at right hand side to the present value of variable on left side and assigns it back.p := 2
p += 3 // p will be 5
Same as p = p + 3
-=Subtraction assignment operatorSubtracts the value at right hand side from the present value of variable on left side and assigns it back.p := 10
p -= 2 // p will be 8
Same as p = p – 2
*=Product
assignment operator
Multiplies the value at right hand side to the present value of variable on left side and assigns it back.q := 3
q *= 2 // q will be 6
Same as q = q * 2
/=Division
assignment operator
Divides the present value of variable on left side with value at right hand side and assigns the quotient back.t := 15
t /= 5 // t will be 3
Same as t = t / 5
%=Modulus assignment operatorDivides the present value of variable on left side with value at right hand side and assigns the remainder back.x := 14
x %= 5 // x will be 4
Same as x = x % 5
|=Bitwise OR assignment operatorPerforms bit wise OR operation between the present value of variable on left side and value at right hand side and assigns the result back.p := 10
p |= 20 // p will be 30
Same as p = p | 20
&=Bitwise AND assignment operatorPerforms bit wise AND operation between the present value of variable on left side and value at right hand side and assigns the result back.p := 5
p &= 20 // p will be 4
Same as p = p & 20
^=Exclusive OR assignment operatorPerforms exclusive OR operation between the present value of variable on left side and value at right hand side and assigns the result back.p := 5
p ^= 20 // p will be 17
Same as p = p ^ 20
<<=Left shift assignment operatorLeft shifts present value of variable on left side to the number of places at right hand side and assigns the result back.p := 5
p <<= 3 // p will be 40
Same as p = p << 3
>>=Right shift assignment operatorRight shifts present value of variable on left side to the number of places at right hand side and assigns the result back.p := 8
p >>= 1 // p will be 1
Same as p = p >> 3

Golang supports pointers and hence following address operators related to pointers are available in it.

OperatorMeaningExample
&Returns the pointer to the following operand.p := 10
&p
*Returns the value at address of the pointer.p := 10
q := &p
*q will be 10

Channel operators
Following are the operators that are applicable to variables of Golang channel type.

OperatorNameMeaningExample
<-Channel receive operatorAssigns value received from channel to variable on left.val <-ch
where ch is a Golang channel

0

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.