Golang Operators

Learn via video courses
Topics Covered

Overview

In the programming world, the operator is considered a symbol that is used for performing different operations on the value or any variable in the Go language. In this article, we will be learning about different operators in the Go language along with their examples.

What are the Operators in Golang?

The operator in any programming language is a foundation and thus is an important part of the Go language. The Go language has functionality incomplete without operators. The use of operators allows the developers to perform various different kinds of operations on the operand.

What Are the Different Types of Operators in Golang?

The operators in the Go language are of a different type that is categorized into the following type on the basis of the functionality that the operator provides:

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Miscellaneous operators

Arithmetic Operators

These are used for performing different arithmetic and mathematical operations on variables or operands in the Go language.

Description of Different Operators

  • Addition- It is used for adding two numbers.
  • Subtraction- It is used for subtracting two numbers.
  • Multiplication- It is used for multiplying two numbers.
  • Division- It is used for dividing one number by another.
  • Modulo Division- It is used for dividing two numbers and the resultant reminder is the output of the modulo division operation.

Symbols

OperatorsExample
+ (Addition)a + b
- (Subtraction)a - b
* (Multiplication)a * b
/ (Division)a / b
% (Modulo Division)a % b

Example

OUTPUT

Relational Operators

This operator is used for comparing two different values or variables.

In this, the == operator is the relational operator that is used for checking if the values a and b are equal or not. The relational operator returns true if the two values are equal else it returns false if the two values are not equal.

Description of Different Operators

OperatorExampleDescriptions
== (equal to)a == breturns true if a and b are equal
!= (not equal to)a != breturns true if a and b are not equal
> (greater than)a > breturns true if a is greater than b
< (less than)a < breturns true if a is less than b
>= (greater than or equal to)a >= breturns true if a is either greater than or equal to b
<= (less than or equal to)a <= breturns true if a is either less than or equal to b

Symbols

Operator

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

Example

Assume a=10 and b=20

OperationResult
a==bfalse
a!=btrue
a<btrue
a<=btrue
a>bfalse
a>=bfalse

Logical Operators

These operators are used for performing logical related operations. Depending upon the condition, the logical operator returns true or false.

Description of Different Operators

OperatorDescriptionExample
&& (Logical AND)exp1 && exp2returns true if both expressions exp1 and exp2 are true
|| (Logical OR)exp1 || exp2returns true if any one of the expressions is true.
! (Logical NOT)!expreturns true if exp is false and returns false if exp is true.

Symbols

Operator

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)

Example

No.OperationResult
1(a<b) && (c>b)true
2(a>b) && (c>b)false
3(a>b) || (c>b)true
4(a<b) || (c>b)true

Bitwise Operators

There are 6 bitwise operators in the Go language and it works at a bit level or performs bit-by-bit operations.

Description of Different Operators

Assume int a=8 and b=13

Operatorexampledescription
& (bitwise AND)a&b=8It takes 2 numbers as an operand and performs AND operation on every bit of the following two numbers. The output of the operator AND is 1, if and only if both the bits are 1.
| (bitwise OR)a|b=13It also takes two numbers as an operand and does the OR operation on each and every bit of the two numbers. The output of the OR bit is 1, if and only if one among the following two operands is 1.
^ (bitwise XOR)a^b=5It also takes two number as an operand and perform the XOR operation on each and every bit of the two number. The XOR output is 1, if and only if the two bits are different.
<< (left shift)a<<1=16It also takes two numbers as an argument, it left shifts the bit of first operand and then decides the number of places to sift for the second operand.
>> (right shift)a>>2=2It also takes two numbers as an argument and performs the right shift on the first operand and then decides the number of bits to be shifted for the second operand.
&^ (AND NOT)a&^b=0This operator is used for clearing the bit.

Symbols

  • & (bitwise AND)
  • | (bitwise OR)
  • ^ (bitwise XOR)
  • << (left shift)
  • >> (right shift)
  • &^ (AND NOT)

Example

Go program to illustrate the use of bitwise operators

OUTPUT

Assignment Operators

These operators are used for assigning the value to a variable. The left side of the operand contains the variable whereas the right side contains the assignment operator along with the value. The error is popped up if the data type and variable are not of the same type.

Description of Different Operators

OperatorExampleSame as
+= (addition assignment)a += ba = a + b
-= (subtraction assignment)a -= ba = a - b
*= (multiplication assignment)a *= ba = a * b
/= (division assignment)a /= ba = a / b
%= (modulo assignment)a %= ba = a % b

Symbols

Operator

  • += (addition assignment)
  • -= (subtraction assignment)
  • *= (multiplication assignment)
  • /= (division assignment)
  • %= (modulo assignment)

Example

OUTPUT

Miscellaneous Operators

There are three types of miscellaneous operators.

Description of Different Operators

  • & : This operator is used for returning the address of the variable.
  • *: This operator is used for providing the pointer to the variable.
  • <- This is also known as receive operator and is used for receiving the value from the channel.

Symbols

OperatorDescriptionExample
&address of a variable is returned at the output.&a; provides actual address of the variable.
*Returns the Pointer to a variable.*a; provides actual pointer to a variable.

Example

OUTPUT

Operator's Precedence in Golang

The operator's precedence in golang is used for determining the grouping of the terms in an expression which affects the expression that is evaluated in golang. There are few operators that have high precedence over other operators, for instance, the multiplication operator has higher precedence when compared to the addition operator in any programming language.

Below given is the list of operators along with their precedence.

OperatorSymbolAssociativity
Multiplicative* / %Left to right
Additive+ -Left to right
Postfix() [] -> . ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight to left
Equality== !=Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Logical OR||Left to right
Logical AND&&Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Assignment= += -= *= /= %=>>= <<= &= ^==
Comma,Left to right

Conclusion

  • The operator in any programming language is a foundation and thus is an important part of the Go language.
  • The Go language has functionality incomplete without operators. The use of operators allows the developers to perform various different kinds of operations on the operand.
  • The operators in the Go language are of different type that is categorized into the following type on the basis of the functionality that the operator provides:
    • Arithmetic operators
    • Relational operators
    • Logical operators
    • Bitwise operators
    • Assignment operators
    • Miscellaneous operators