# 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 –

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators

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

Addition (+) | Adds two operands | x+y returns 7 |

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 |

Example of Arithmetic Operators

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

Example of Relational Operators

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

Example of Assignment Operators

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

Address Of (&) | Returns the address of a variable. | &x |

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 |

Example of Miscellaneous Operators

What's New

- C Language MCQs
- Python MCQs
- Perl MCQs
- MongoDB MCQs
- Java MCQs
- C# MCQs
- Scala MCQs
- Blockchain MCQs
- AutoCAD MCQs
- PHP MCQs
- JavaScript MCQs
- jQuery MCQs
- ReactJS MCQs
- AngularJS MCQs
- JSON MCQs
- Ajax MCQs
- SASS MCQs
- HTML MCQs
- Advanced CSS MCQs
- CSS MCQs
- PL/SQL MCQs
- SQL MCQs
- MS Word MCQs
- PL/SQL MCQs
- SQL MCQs
- Software Engineering MCQs
- Data Analytics and Visualization MCQs
- MIS MCQs
- Linux MCQs
- WordPress MCQs
- Blogging MCQs

- Generally Accepted Accounting Principles MCQs
- Bills of Exchange MCQs
- Business Environment MCQs
- Sustainable Development MCQs
- Marginal Costing and Absorption Costing MCQs
- Globalisation MCQs
- Indian Economy MCQs
- Retained Earnings MCQs
- Depreciation MCQs
- Partnership MCQs
- Sole Proprietorship MCQs
- Goods and Services Tax (GST) MCQs
- Cooperative Society MCQs
- Capital Market MCQs
- Business Studies MCQs
- Basic Accounting MCQs
- MIS Executive Interview Questions
- Go Language Interview Questions

Top Interview Coding Problems/Challenges!

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions!