Site icon codippa

Operators

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.

Operator Name Meaning Example
+ Addition Used for addition of two or more values sum := 2 + 5
Subtraction Used for subtracting one value from another diff := 10 – 2
* Multiplication Multiplying two or more values p := 5 * 2 * 9
/ Division For dividing two numbers. It returns the quotient of division. q := 10/5
Result will be 2
% Modulus Remainder of division of two numbers. r := 11/3
Result will be 2
++ Increment For increasing the value of preceding variable by 1. p := 1
p++
p will be 2
Decrement For reducing the value of preceding variable by 1. q := 10
q–
q will not be 9
& Bitwise AND Performs AND operation on its operands bit by bit. p := 11
q := 21
p & q will be 1
| Bitwise OR Performs AND operation on its operands bit by bit. p := 10
q := 20
p | q will be 30
<< Left shift Left 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 shift Right 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.

Operator Name Meaning Example
< Less than For 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 than For 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 to For 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 to For 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 to For 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 to For 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.

Operator Name Meaning Example
&& Conditional AND For 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 OR For 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
! NOT Used 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.

Operator Name Meaning Example
= Assignment Assigns value at right side to variable on left. var p int
p = 2
:= Declaration operator Declares a variable and assigns value to it. With this operator, a variable is declared and initialized in the same statement. p := 2
+= Addition assignment operator Adds 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 operator Subtracts 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 operator Divides 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 operator Performs 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 operator Performs 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 operator Performs 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 operator Left 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 operator Right 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

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

Operator Meaning Example
& 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.

Operator Name Meaning Example
<- Channel receive operator Assigns value received from channel to variable on left. val <-ch
where ch is a Golang channel

Exit mobile version