# Go Operators

Operators in Programming languages are the special symbol that tells the compiler to perform specific operations on operands. Operators are the core foundation and building blocks of any programming language.

In Go language operators can be categorized into five different types:

- Arithmetic operators
- Logical operators
- Bitwise operators
- Relational operators
- Assignment operators

## Arithmetic operators

The Arithmetic operators are mainly used to perform Mathematical/Arithmetic operations such as addition, subtraction, multiplication, and division. Following is the list of Arithmetic operators in Golang:

`+`

to add two integers Eg:`a + b`

,`-`

to subtract two integers`a - b`

,`*`

to multiply two integers`a * b`

,`/`

to divide two integers (discarding the remainder)`a / b`

,`%`

to get the remainder of the division of two integers`a % b`

.

Let us look at an example to demonstrate Arithmetic operators.

```
// Program to illustrate Arithmetic operators
package main
import "fmt"
func main() {
x := 45
y := 15
// Addition
sum := x + y
fmt.Printf("Result of %d + %d = %d\n", x, y, sum)
// Subtraction
difference := x - y
fmt.Printf("Result of %d - %d = %d\n", x, y, difference)
// Multiplication
product := x * y
fmt.Printf("Result of %d * %d = %d\n", x, y, product)
// Division
quotient := x / y
fmt.Printf("Result of %d / %d = %d\n", x, y, quotient)
// Modulus
res := x % y
fmt.Printf("Result of %d mod %d = %d\n", x, y, res)
}
```

**Output**

```
Result of 45 + 15 = 60
Result of 45 - 15 = 30
Result of 45 * 15 = 675
Result of 45 / 15 = 3
Result of 45 % 15 = 0
```

## Logical operators

Logical operators are mainly used to evaluate the boolean expression and determine whether the entire expression is true or false based on the operator used. Therefore, logical operators are commonly used in decision-making in the Go language.

Following is the list of Logical operators in Golang:

`&&`

for the conjunction (and),`||`

for the disjunction (or),`!`

for the negation (not).

Basically,

`x && y`

is true if and only**if**else its false,`x`

and`y`

are true`x || y`

is true if and only**if**,`x`

or`y`

(or both) are true`!x`

is true if and only**if**.`x`

is false

Let us look at an example to demonstrate Logical operators.

```
// Program to illustrate Logical operators
package main
import "fmt"
func main() {
x := 45
y := 15
// Logical AND
if x != 0 && x > y {
fmt.Println("True")
} else {
fmt.Println("False")
}
// Logical OR
if x == 45 || x == -42 {
fmt.Println("True")
} else {
fmt.Println("False")
}
// Logical NOT
if x != 45 {
fmt.Println("True")
} else {
fmt.Println("False")
}
}
```

**Output**

```
True
True
False
```

## Bitwise operators

Bitwise operators enable you to manipulate the individual raw data bits within a data structure.

Following is the list of Bitwise operators in Golang:

`&`

is a Binary AND operator,`|`

is a Binary OR operator,`^`

is a Binary XOR operator,`<<`

is a Binary Left Shift operator,`>>`

is a Binary Right Shift operator,`&^`

is a bit clear (AND NOT).

Let us look at an example to demonstrate Bitwise operators.

```
// Program to illustrate Bitwise operators
package main
import "fmt"
func main() {
x := 45
y := 15
// & (bitwise AND)
res1 := x & y
fmt.Printf("Result of %d & %d = %d\n", x, y, res1)
// & (bitwise OR)
res2 := x | y
fmt.Printf("Result of %d | %d = %d\n", x, y, res2)
// ^ (bitwise XOR)
res3 := x ^ y
fmt.Printf("Result of %d ^ %d = %d\n", x, y, res3)
// << (Left Shift)
res4 := x << y
fmt.Printf("Result of %d << %d = %d\n", x, y, res4)
// >> (Right Shift)
res5 := x >> y
fmt.Printf("Result of %d >> %d = %d\n", x, y, res5)
// &^ (AND NOT)
res6 := x &^ y
fmt.Printf("Result of %d &^ %d = %d\n", x, y, res6)
}
```

**Output**

```
Result of 45 & 15 = 13
Result of 45 | 15 = 47
Result of 45 ^ 15 = 34
Result of 45 << 15 = 1474560
Result of 45 >> 15 = 0
Result of 45 &^ 15 = 32
```

## Relational operators

Relational operators are mainly used to test or define some kind of relation between two entities. In simple terms, it compares the two values or variables using a specific operator.

Following is the list of Relational operators in Golang:

`==`

is equal to operator. Eg:`x == y`

returns true if and only if both`x`

and`y`

are equal`!=`

is not equal to operator. Eg:`x != y`

returns true if and only if`x`

and`y`

are not equal`>`

is a greater than operator. Eg:`x > y`

returns true if and only if`x`

is greater than`y`

`<`

is a less than operator. Eg:`x < y`

returns true if and only if`x`

is less than`y`

`>=`

is greater than or equal to operator. Eg:`x > y`

returns true if and only if`x`

is greater than or equal`y`

`<=`

is less than or equal to operator. Eg:`x < y`

returns true if and only if`x`

is less than or equal to`y`

Let us look at an example to demonstrate Relational operators.

```
// Program to illustrate Relational operators
package main
import "fmt"
func main() {
x := 45
y := 15
// Equal to (==)
res1 := x == y
fmt.Printf("Result of %d == %d = %t\n", x, y, res1)
// Not Eqaul to
res2 := x != y
fmt.Printf("Result of %d != %d = %t\n", x, y, res2)
// greater than
res3 := x > y
fmt.Printf("Result of %d > %d = %t\n", x, y, res3)
// Lesser than
res4 := x < y
fmt.Printf("Result of %d < %d = %t\n", x, y, res4)
// greater than equal to
res5 := x >= y
fmt.Printf("Result of %d >= %d = %t\n", x, y, res5)
// lesser than equal to
res6 := x <= y
fmt.Printf("Result of %d <= %d = %t\n", x, y, res6)
}
```

**Output**

```
Result of 45 == 15 = false
Result of 45 != 15 = true
Result of 45 > 15 = true
Result of 45 < 15 = false
Result of 45 >= 15 = true
Result of 45 <= 15 = false
```

## Assignment operators

Assignment Operators are mainly used to assign a value to the variables. For Example:

`var number int = 5`

Here the `= `

is the **simple assignment operator** that assigns the value 5 to the variable `number`

Following is the list of Assignment operators in Golang:

Operator | Expression | Example |
---|---|---|

`=` (Simple Assignment) | `z=x+y` | `z=x+y` |

`+=` (Addition Assignment) | `x+=y` | `x=x+y` |

`-=` (Subtraction Assignment) | `x-=y` | `x=x-y` |

`*=` (Multiplication Assignment) | `x*-y` | `x=x*y` |

`/=` (Division Assignment) | `x/=y` | `x=x/y` |

`%=` (Modulus Assignment) | `x%=y` | `x=x%=y` |

`<<=` (Left Shift AND Assignment) | `x<<=y` | `x=x<<=y` |

`>>=` (Right Shift AND Assignment) | `x>>=y` | `x=x>>=y` |

`&=` (Bitwise AND Assignment) | `x&=y` | `x=x&=y` |

`^=` (Bitwise Exclusive OR) | `x^=y` | `x=x^=y` |

`|=` (Bitwise Inclusive OR) | `x|=y` | `x=x|=y` |

Let us look at an example to demonstrate Assignment operators

```
// Program to illustrate Assignment operators
package main
import "fmt"
func main() {
var x = 45
var y = 15
// (Simple Assignment)
x = y
fmt.Println(x)
// (Addition Assignment)
x += y
fmt.Println(x)
// (Subtraction Assignment)
x -= y
fmt.Println(x)
// (Multiplication Assignment)
x *= y
fmt.Println(x)
// (Division Assignment)
x /= y
fmt.Println(x)
// (Modulus Assignment)
x %= y
fmt.Println(x)
}
```

**Output**

```
15
30
15
225
15
0
```