Monday, November 19, 2018

Learn Golang Tutorials - Operators Guide with examples

Go Language Operators

Golang Operator tutorials with examples
Like many programming languages, Golang has a support for various inbuilt operators.

Important keynotes of operators in the Go language
  • Operators are character sequence used to execute some operations on a given operand(s)
  • Each operator in 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
The syntax of Operators
Here is an example Unary Operator
Operand Operator
Here is an example Binary Operator which contains Two operands
Operand1 Operator Operand2
The operand is data or variable that need to be manipulated

Arithmetic Operators 

Arithmetic operators perform arithmetic calculations like addition, multiplication, subtract, on numeric values. Assume that Operands a and b values are 10,5.
a%bRemain
Symbol Name Usage Description Example
+ Addition a+b Sum of two values 10+5=5
- Subtraction a-b Subtract two values 10-5=5
* Multiplication a*b Multiply two values 10*5=50
/ Quotient a/b Divide Operand by Denomintions 10/5=2
% ModulusdThe remainderafter appliing Quotient 10%5=0
++ Increment a+= Increment value by One 10++=11
-- Decrement a-- Decrement value by One 10--=9
Four operators (+,-,*,/) operates on Numeric types- Integer and Float,Complex types + operatos on String, ++ and -- operats on Numeric types
Following is an example for usage of Arthmatic 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)
}
Output of 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

Comparision or Relational Operators 


Comparision operators are used to compare the operands in a expression. Operands can be named type and compared operand of same type or values of same type These operators enclosed in ( and ) ie (a==b) If it not enlosed - a == b gives compilation error - cannot use a == b (type bool) as type int in assignment Operands of any type as mentioned in below key notes. and returned value of this comparision is untyped boolean value - true or false.

Key notes of Comparision Operators
  • All primitive types (Integers,Floats,Boolean,String) are comparable
  • Complex data types, Channel Pointer can be used to compared with this
  • Interfaces can be comparable and return true - if both interfaces of 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 list of Go Inbuilt Comparision 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 comparison operators Usage 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 above program is compiled and executed ouputs 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

Logical Operators 

Logical operators accepts boolean values and returns boolean value. It contains Left and Right Operands. If Left Operand is evaluted to true, Right Operand will not be evaluted. These are called short circuit rules if both operands (1 && 1)are not boolean, It gives compilation error -invalid operation: 1 && 1 (operator && not defined on untyped number) Following are list of operators supported in Go.
Symbol Name Usage Description Example
&& Logial AND (a&&b) true - if both operands are evaluated to true, false - if one of the operand is evalute to false (true&&true) is true
|| Logial OR (a||b) true - if one of the operand is evaluted to true, false - if both of operands are evaluated to false (false||true) is true
| Logial NOT (a==b) Reverse of the operand evaluated value - true becomes false, false becomes true (!true) is false
Here is an example for 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

Bitwise Operators

These operators are used with bit manipulation. Golangauge has support different bitwise operators. It operats 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 1 0 1 1
1 1 1 1 1
1 0 0 1 1
Following are list of operators supported in Go
Symbol Name Usage Description Example
& Bitwise AND (a&b) Copies a bit result if both operands exists (10&5) is 0
| Bitwise OR (a|b) Copies a bit to the output Atleast one operand exists (10|5) is 10
^ Bitwise XOR (a^b) Copies bit to Output, if exist aleast in One Operand and Not exists in both (10^5) is 15
&^ AND NOT (a&^b) Compination 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 usage
package main

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

Assignment Operators

Assignment operators are used to perform calculation of some operations and finally result is assigned to left side operand. Goland has support for multiple assignment operators
Symbol Name Description
= Assignment Operator It will assign value from right operand to Left operand
+= Addition AND Assignment Operator First Right Operand is added with Left Operand and result is assigned to Left Operand
-= Substract AND Assignment Operator First Right Operand is substracted with Left Operand and result is assigned to Left Operand
*= Multiply AND Assignment Operator First Right Operand is muliltplied 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 is 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 Substraction 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 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 Substraction AND Assignment Operator 10
4# Output of Multiply AND Assignment Operator 100
5# Output of Divide AND Assignment Operator 10
6# Output of Moudulus 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

Address Operators 

There are two operators related address of a variable
asterisk * Operator 
This is used to give pointer of a variable and dereference pointer which gives pointer to point of points
Ambersand & Operator 
This gives address of a variable. This gives actual location of variable saved in memory.
Here is an example of Asterisk and Ambersand 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)

}
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

Operator Precedence 

In any expression, multiple operators are applied, Precedence decides evaluation order on which operators runs first. Unary Operators ranks highest precedence than binary operators. You can check official documentation here.
Prcedence Rank Operators
5 * / % << >> & &^
4 + - | ^
3 == != < <= > >=
2 &&
1 ||

Related article


EmoticonEmoticon