Learn Golang Operators Guide with examples
Go Language Operators
Like many programming languages, Golang
has support for various inbuilt operators.
Important keynotes of operators in the Go language
- Operators are character sequences used to execute some operations on a given operand(s)
- Each operator in the Go language is of types Unary Operator or Binary Operator. Binary operators accept two operands, Unary Operator accepts one operand
- Operators operate on one or two operands with expressions
- These are used to form expressions
The following are different types covered as part of this blog post.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operator
- Address Operators
- Other Operators
- Operator Precedence
Operators syntax:
There are two types of operators
unary
- applies to the single operandbinary
- applies to two operands.
Here is a Unary
Operator Syntax
Operand Operator
Here is a Binary
Operator Syntax
Operand1 Operator Operand2
The operand is data or variables that need to be manipulated.
Golang Arithmetic Operators
Arithmetic
operators perform arithmetic calculations like addition, multiplication, subtract, on numeric values. Assume that Operands a and b values are 10,5.
Symbol | Name | Usage | Description | example |
---|---|---|---|---|
+ | Addition | a+b | Sum of two values | 10+5=15 |
- | Subtraction | a-b | Subtract two values | 10-5=5 |
* | Multiplication | a*b | Multiply two values | 10*5=50 |
/ | Quotient | a/b | Divide Operand by Denomination | 10/5=2 |
% | Modulus | a%b | The remainder after applying Quotient | 10%5=0 |
++ | Increment | a+= | Increment value by One | 10++=11 |
-- | Decrement | a-- | Decrement value by One | 10--=9 |
Four operators (+,-,*,/) operate on Numeric types such as Integer
and Float
, Complex
. +
operator on String. ++
and --
operators on Numeric types.
Following is an example for the usage of Arithmetic operators
package main
import "fmt"
func main() {
var a int = 100
var b int = 50
var result int
result = a + b
fmt.Printf("Output of Plus Operator %d\n", result)
result = a - b
fmt.Printf("Output of Minus Operator %d\n", result)
result = a * b
fmt.Printf("Output of Star Operator %d\n", result)
result = a / b
fmt.Printf("Output of Divide Operator %d\n", result)
result = a % b
fmt.Printf("Output of Remainder Operator %d\n", result)
a++
fmt.Printf("Output of Increment Operator %d\n", a)
a--
fmt.Printf("Output of Decrement Operator %d\n", a)
}
The output of the above program code execution is
Output of Plus Operator 150
Output of Minus Operator 50
Output of Star Operator 5000
Output of Divide Operator 2
Output of Remainder Operator 0
Output of Increment Operator 101
Output of Decrement Operator 100
Golang Comparison or Relational Operators
Comparison
operators are used to compare the operands in an expression. Operands can be named type and compared operand of same type or values of the same type.
These operators enclosed in (
and )
i.e (a==b)
, If it is not enclosed - a == b
gives compilation error - cannot use a == b (type bool) as type int in assignment Operands of any type as mentioned in below keynotes. And returned value of this comparison is untyped a boolean value - true or false.
Keynotes of Comparison Operators
- All primitive types (Integers, Floats, Boolean, String) are comparable
- Complex data types, Channel Pointer can be used to compare with these
- Interfaces can be comparable and return true - if both interfaces are of the same dynamic type, values, or nil, else return false
- if Structs can be comparable and returns true - properties or fields are equal
- if arrays compared with this, returns true - if both array values are equal
Following are a list of Go Inbuilt Comparison Operators
Symbol | Name | Usage | Description | example |
---|---|---|---|---|
== | Identical or equal | (a==b) | Checks and compare two values, true - if both are equal, false - if both are not equal | (10==5) is false |
!= | Not equal | (a!=b) | Checks and compare two values, true - if both are not equal, false - if both are equal | (10!=5) is true |
> | Greater Than | (a>b) | Checks and First value is greater than second value, return true, else false is returned | (10>5) is true |
>= | Greater Than Equal | (a>=b) | Checks and First value is greater than or equal second value, return true, else false is returned | (10>=5) is true |
< | Lesser Than | (a<b) | Checks and First value is lesser than second value, return true, else false is returned | (11<5) is false |
<= | Lesser ThanEqual | (a<=b) | Checks and First value is lesser than equal second value, return true, else false is returned | (10<=5) is true |
Below is a Golang comparison operators example
package main
import "fmt"
func main() {
var a int = 100
var b int = 50
var result bool
result = (a == b)
fmt.Printf("1# Output of Equal Operator %t\n", result)
result = (a != b)
fmt.Printf("2# Output of Not Equal Operator %t\n", result)
result = (a < b)
fmt.Printf("3# Output of Less Than Operator %t\n", result)
result = (a <= b)
fmt.Printf("4# Output of Less Than Equal Operator %t\n", result)
result = (a > b)
fmt.Printf("5# Output of Greater Than Operator %t\n", result)
result = (a >= b)
fmt.Printf("6# Output of Greater Than Equal Operator %t\n", result)
}
When the above program is compiled and executed outputs the below results
1# Output of Equal Operator false
2# Output of Not Equal Operator true
3# Output of Less Than Operator false
4# Output of Less Than Equal Operator false
5# Output of Greater Than Operator true
6# Output of Greater Than Equal Operator true
Golang Logical Operators
Logical
operators accept the Boolean value and return a Boolean value.
It contains Left and Right Operands. If Left Operand is evaluated to true, Right Operand will not be evaluated.
These are called short circuit rules, if both operands (1 && 1)are not Boolean, and gives compilation error invalid operation: 1 && 1 (operator && not defined on untyped number)
Following is a list of operators supported in the Go language.
Symbol | Name | Usage | Description | example |
---|---|---|---|---|
&& | Logical AND | (a&&b) | true - if both operands are evaluated to true, false - if one of the operand is evaluated to false | (true&&true) is true |
|| | Logical OR | (a || b) | true - if one of the operand is evaluated to true, false - if both of operands are evaluated to false | (false || true) is true |
! | Logical NOT | (a!b) | Reverse of the operand evaluated value - true becomes false, false becomes true | (!true) is false |
Here is an example of Logical Operator usage
package main
import "fmt"
func main() {
var operand1 bool = true
var operand2 bool = false
var result bool
result = (operand1 && operand2)
fmt.Printf("1# Output of Conditional AND Operator %t\n", result)
result = (operand1 || operand1)
fmt.Printf("2# Output of Conditional OR Operator %t\n", result)
result = (!operand1)
fmt.Printf("3# Output of Conditional NOT Operator %t\n", result)
}
Compilation and running of the above is
1# Output of Conditional AND Operator false
2# Output of Conditional OR Operator true
3# Output of Conditional NOT Operator false
Golang Bitwise Operators
These operators are used with bit manipulation. Go language has supported different bitwise operators. It operates on bits only. Generate true table manipulation values on bits 0 and 1
Operand1 | Operand2 | Operand1 & Operand2 | `Operand1 | Operand2` | Operand1 ^ Operand2 |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 | 0 |
1 | 1 | 1 | 1 | 1 | 1 |
1 | 0 | 0 | 1 | 1 | 1 |
Following is a List of Bitwise Operators supported in Go
Symbol | Name | Usage | Description | example |
---|---|---|---|---|
& | Bitwise AND | (a&b) | Copies a bit of result if both operands exists | (10&5) is 0 |
| | Bitwise OR | (a | b) | Copies a bit to the output At least one operand exists | (10 | 5) is 10 |
^ | Bitwise XOR | (a^b) | Copies bit to Output, if exist at least in One Operand and Not exists in both | (10^5) is 15 |
&^ | AND NOT | (a&^b) | Combination AND operator and Bitwise XOR | (10&>5) is 10 |
<< | Left Shift | (a<<b) | Left First Operand is moved by number of bits number with Right operand | (10<<5) is 320 |
>> | Right Shift | (a>>b) | Right Operand is moved by number of bits number with Left operand | (10>>5) is 0 |
Here is an example for Logical Operator example
package main
import "fmt"
func main() {
var operand1 uint = 10 // bits are 0000 1010
var operand2 uint = 5 // bits are 0000 0011
var result uint = 0
result = (operand1 & operand2)
fmt.Printf("1# Output of Bitwise AND Operator %d\n", result)
result = (operand1 | operand1)
fmt.Printf("2# Output of Bitwise OR Operator %d\n", result)
result = (operand1 ^ operand2)
fmt.Printf("3# Output of Bitwise XOR Operator %d\n", result)
result = (operand1 &^ operand2)
fmt.Printf("4# Output of AND NOT Operator %d\n", result)
result = (operand1 << operand2)
fmt.Printf("5# Output of Left Shift Operator %d\n", result)
result = (operand1 >> operand2)
fmt.Printf("6# Output of Right Shift Operator %d\n", result)
}
Compilation and running of the above is
1# Output of Bitwise AND Operator 0
2# Output of Bitwise OR Operator 10
3# Output of Bitwise XOR Operator 15
4# Output of AND NOT Operator 10
5# Output of Left Shift Operator 320
6# Output of Right Shift Operator 0
Golang Assignment Operators
Assignment
operators are used to perform the calculation of some operations and finally result is assigned to the left side operand.
Golang has support for multiple assignment operators
Symbol | Name | Description |
---|---|---|
= | Assignment Operator | It will assign a value from right operand to Left operand |
+= | Addition AND Assignment Operator | First Right Operand is added with Left Operand and the result is assigned to Left Operand |
-= | subtract AND Assignment Operator | First Right Operand is subtracted with Left Operand and the result is assigned to Left Operand |
*= | Multiply AND Assignment Operator | First Right Operand is multiplied with Left Operand and result is assigned to Left Operand |
/= | Divide AND Assignment Operator | First Right Operand is Divided with Left Operand and result is assigned to Left Operand |
%= | Modulus AND Assignment Operator | First Right Operand has applied Modulus operator with Left Operand and result is assigned to Left Operand |
<<= | Left Shift AND Assignment Operator | Applies Left Shift on operands and assign the result to Left Operand |
>>= | Right Shift AND Assignment Operator | Applies Right Shift on operands and assign the result to Right Operand |
&= | Bitwise AND Assignment Operator | Applies Bitwise AND on operands and assign the result to Right Operand |
|= | Bitwise OR AND Assignment Operator | Applies Bitwise OR on operands and assign the result to Right Operand |
^= | Bitwise XOR AND Assignment Operator | Applies Bitwise XOR on operands and assign the result to Right Operand |
Following is an example of Using assignment Operators
package main
import "fmt"
func main() {
var operand1 int = 10
var result = 0
result = operand1
fmt.Printf("1# Output of Assignment Operator %d\n", result)
result += operand1
fmt.Printf("2# Output of Addition AND Assignment Operator %d\n", result)
result -= operand1
fmt.Printf("3# Output of Subtraction AND Assignment Operator %d\n", result)
result *= operand1
fmt.Printf("4# Output of Multiply AND Assignment Operator %d\n", result)
result /= operand1
fmt.Printf("5# Output of Divide AND Assignment Operator %d\n", result)
result %= operand1
fmt.Printf("6# Output of Moudulus AND Assignment Operator %d\n", result)
result &= operand1
fmt.Printf("7# Output of Bitwise And AND Assignment Operator %d\n", result)
result |= operand1
fmt.Printf("8# Output of Bitwise OR AND Assignment Operator %d\n", result)
result ^= operand1
fmt.Printf("9# Output of Bitwise XOR AND Assignment Operator %d\n", result)
}
When the above program code is compiled and executed, Output is
1# Output of Assignment Operator 10
2# Output of Addition AND Assignment Operator 20
3# Output of subtraction AND Assignment Operator 10
4# Output of Multiply AND Assignment Operator 100
5# Output of Divide AND Assignment Operator 10
6# Output of Modulus AND Assignment Operator 0
7# Output of Bitwise And AND Assignment Operator 0
8# Output of Bitwise OR AND Assignment Operator 10
9# Output of Bitwise XOR AND Assignment Operator 0
Golang Address Operators
There are two operators related address of a variable asterisk *
Operator These are used to give a pointer of a variable and dereference pointer which gives a pointer to a point of points.
Ampersand &
Operator This gives the address of a variable. It gives the actual location of the variable saved in memory. Here is an example of Asterisk and Ampersand Operator
package main
import "fmt"
func main() {
var v1 int = 12
var v2 int32
var v3 float32
var v4 string
var v5 bool
var pointerVar *int
fmt.Printf("1# Variable Data Type %T\n", v1)
fmt.Printf("2# Variable Data Type %T\n", v2)
fmt.Printf("3# Variable Data Type %T\n", v3)
fmt.Printf("4# Variable Data Type %T\n", v4)
fmt.Printf("5# Variable Data Type %T\n", v5)
fmt.Printf("6# variable v1 value %d\n", v1)
fmt.Printf("7# Address of varible v1 %d\n", &v1)
pointerVar = &v1
fmt.Printf("8# variable pointerVar value %d\n", *pointerVar)
fmt.Printf("9# Address of varible pointerVar %d\n", &pointerVar)
}
The output of the above programs is
1# Variable Data Type int
2# Variable Data Type int32
3# Variable Data Type float32
4# Variable Data Type string
5# Variable Data Type bool
6# variable v1 value 12
7# Address of varible v1 824634048600
8# variable pointerVar value 12
9# Address of varible pointerVar 824634212376
Golang Operator Precedence
In any expression, multiple operators are applied, Precedence decides the evaluation order on which operators run first. Unary Operators rank the highest precedence than binary operators. You can check official documentation [here]https://golang.org/ref/spec🔗.
Precedence Rank | Operators |
---|---|
5 | * / % << >> & &^ |
4 | + -| ^ |
3 | == != < <= > >= |
2 | && |
1 | || |