Operators in Golang

  • date 22nd August, 2022 |
  • by Prwatech |
  • 0 Comments

Go Operators (With Examples)

 

In Go (or Golang), operators are symbols or keywords that perform specific operations on operands, which can be variables, constants, or expressions. Go supports a variety of operators categorized into different types, including arithmetic operators, comparison operators, logical operators, assignment operators, and more.

Arithmetic operators (+, -, *, /, %) are used for basic arithmetic operations like addition, subtraction, multiplication, division, and modulus. Comparison operators (==, !=, <, >, <=, >=) are used to compare values and produce boolean results based on the comparison.

Logical operators (&&, ||, !) are use to perform logical operations like AND, OR, and NOT on boolean values. Bitwise operators (&, |, ^, <<, >>, &) are used to perform operations at the bit level.

Assignment operators (=, +=, -=, *=, /=, %=) are use to assign values to variables and perform compound assignment operations.

Understanding and utilizing operators is essential for writing expressive and efficient Go programs. Operators allow developers to manipulate data, control program flow, and perform computations effectively. Mastery of Go operators enables developers to write concise and readable code while leveraging the language’s powerful capabilities for data manipulation and control.

1. Arithmetic Operators

List of different arithmetic operators in Go Language:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulus

Ex:

package main
import "fmt"
func main() {
    var x, y, z int
    x = 10
    y = 20
    z = x * y
fmt.Println("Addition of x & y :", x + y)
fmt.Println("Subtraction of x & y :", x - y)
fmt.Println("Multiplication of no is ", z)
fmt.Println("division of x & y :", x/y)
} 

Output:

PS C:\GO_Language\operators> go run opr1.go
Addition of x & y : 30
Subtraction of x & y : -10
Multiplication of no is  200
division of x & y : 0

2. Relational operators

package main
import "fmt"

func main() {
  	var a, b int
    a, b: = 10, 20

    fmt.Println(a == b)
    fmt.Println(a != b)
    fmt.Println(a < b)
    fmt.Println(a > b)
    fmt.Println(a >= b)
    fmt.Println(a <= b)
}

Output :

PS C:\GO_Language\operators> go run opr2.go
false
true
true
false
false
true

3. 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 :

PS C:\GO_Language\operators> go run opr3.go
True
True
True

4.Bitwise Operators:

package main
import "fmt"
func main() {
    x: = 10
    y: = 12
    var z int
    z = x & y
    fmt.Println("x & y :", x, y)
    z = x | y
    fmt.Println(" x | y :", z)
    z = x ^ y
    fmt.Println("x ^ y	:", z)
    z = x << y
    fmt.Println("x << y	:", z)
    z = x >> y
    fmt.Println("x >> y	:", z)
}

Output :

PS C:\GO_Language\operators> go run opr4.go
x & y : 10 12
 x | y : 14
x ^ y   : 6
x << y  : 40960
x >> y  : 0

5. 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 :

PS C:\GO_Language\operators> go run opr5.go
50
100
50
2500
50
0

6.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 :

PS C:\GO_Language\operators> go run opr6.go
4
7

Go Operators (With Examples)

0
0

Quick Support

image image