• Share Ideas, Start Something Good.

### Operators in Golang

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

# 1. Arithmetic Operators

List of different arithmetic operators in Go Language:

• 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)

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