Open In App

Go Operators

Operators are the foundation of any programming language. Thus the functionality of the Go language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In the Go language, operators Can be categorized based on their different functionality:

Arithmetic Operators

These are used to perform arithmetic/mathematical operations on operands in Go language: 



Note: -, +, !, &, *, <-, and ^ are also known as unary operators and the precedence of unary operators is higher. ++ and — operators are from statements they are not expressions, so they are out from the operator hierarchy.

Example: 






// Go program to illustrate the
// use of arithmetic operators
package main
   
import "fmt"
   
func main() {
   p:= 34
   q:= 20
      
   // Addition
   result1:= p + q
   fmt.Printf("Result of p + q = %d", result1)
      
   // Subtraction
   result2:= p - q
   fmt.Printf("\nResult of p - q = %d", result2)
      
   // Multiplication
   result3:= p * q
   fmt.Printf("\nResult of p * q = %d", result3)
      
   // Division
   result4:= p / q
   fmt.Printf("\nResult of p / q = %d", result4)
      
   // Modulus
   result5:= p % q
   fmt.Printf("\nResult of p %% q = %d", result5)
}

Output: 

Result of p + q = 54
Result of p - q = 14
Result of p * q = 680
Result of p / q = 1
Result of p % q = 14

Relational Operators

Relational operators are used for the comparison of two values. Let’s see them one by one:

Example: 




// Go program to illustrate the
// use of relational operators
package main
   
import "fmt"
   
func main() {
   p:= 34
   q:= 20
      
   // ‘=='(Equal To)
   result1:= p == q
   fmt.Println(result1)
      
   // ‘!='(Not Equal To)
   result2:= p != q
   fmt.Println(result2)
      
   // ‘<‘(Less Than)
   result3:= p < q
   fmt.Println(result3)
      
   // ‘>'(Greater Than)
   result4:= p > q
   fmt.Println(result4)
      
   // ‘>='(Greater Than Equal To)
   result5:= p >= q
   fmt.Println(result5)
      
   // ‘<='(Less Than Equal To)
   result6:= p <= q
   fmt.Println(result6)
      
      
}

Output: 

false
true
false
true
true
false

Logical Operators

They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration.  

Example: 




// Go program to illustrate the
// use of logical operators
package main
import "fmt"
func main() {
    var p int = 23
    var q int = 60
        
    if(p!=q && p<=q){ 
        fmt.Println("True")
    }
        
    if(p!=q || p<=q){ 
        fmt.Println("True")
    }
        
    if(!(p==q)){ 
        fmt.Println("True")
    }
        
}

Output: 

True
True
True

Bitwise Operators

In Go language, there are 6 bitwise operators which work at bit level or used to perform bit by bit operations. Following are the bitwise operators : 

Example: 




// Go program to illustrate the
// use of bitwise operators
package main
   
import "fmt"
   
func main() {
   p:= 34
   q:= 20
      
   // & (bitwise AND)
   result1:= p & q
   fmt.Printf("Result of p & q = %d", result1)
      
   // | (bitwise OR)
   result2:= p | q
   fmt.Printf("\nResult of p | q = %d", result2)
      
   // ^ (bitwise XOR)
   result3:= p ^ q
   fmt.Printf("\nResult of p ^ q = %d", result3)
      
   // << (left shift)
   result4:= p << 1
   fmt.Printf("\nResult of p << 1 = %d", result4)
      
   // >> (right shift)
   result5:= p >> 1
   fmt.Printf("\nResult of p >> 1 = %d", result5)
      
   // &^ (AND NOT)
   result6:= p &^ q
   fmt.Printf("\nResult of p &^ q = %d", result6)
      
      
}

Output: 

Result of p & q = 0
Result of p | q = 54
Result of p ^ q = 54
Result of p << 1 = 68
Result of p >> 1 = 17
Result of p &^ q = 34

Assignment Operators

Assignment operators are used to assigning a value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error. Different types of assignment operators are shown below:

Example:




// Go program to illustrate the
// use of assignment operators
package main
    
import "fmt"
    
func main() {
   var p int = 45
    var q int = 50
       
   // “=”(Simple Assignment)
   p = q
   fmt.Println(p)
       
   // “+=”(Add Assignment)
    p += q
   fmt.Println(p)
       
   //“-=”(Subtract Assignment)
   p-=q
   fmt.Println(p)
       
   // “*=”(Multiply Assignment)
   p*= q
   fmt.Println(p)
       
   // “/=”(Division Assignment)
    p /= q
   fmt.Println(p)
      
    // “%=”(Modulus Assignment)
    p %= q
   fmt.Println(p)
      
}

Output: 

50
100
50
2500
50
0

Misc Operators

Example: 




// Go program to illustrate the
// use of Misc Operators
package main
    
import "fmt"
    
func main() {
  a := 4
     
  // Using address of operator(&) and 
  // pointer indirection(*) operator
  b := &a 
  fmt.Println(*b) 
  *b = 7 
  fmt.Println(a) 
}

Output: 

4
7

 


Article Tags :