## Golang Tutorial Index

Every programming language comes with operators as its base element. None of the functionalities, operations, and calculations can be completed without these. This tutorial will teach you what operators are and the different types of operators in Go programming.

## What Are Operators in Go?

Operators are operating symbols in the Go programming language that instruct the compiler to perform specific operations on operands (variable or constant values) to produce a final result or output. In the following sections, we'll discuss the many different types of operators supported by the Go language.

### Arithmetic Operators

In Go, there are several arithmetic operators that allow you to perform basic mathematical operations, including addition, subtraction, multiplication, and division. Go has the following arithmetic operators:

Operator Description
`+` Addition operator. Adds two operands.
`-` Subtraction operator. Subtracts the second operand from the first.
`*` Multiplication operator. Multiplies the two operands.
`/` Division operator. Divides the first operand by the second.
`%` Modulus operator. Returns the remainder when the first operand is divided by the second.

Here's an example of using Arithmetic Operators in Go:

Example:

``````package main
import "fmt"

func main() {
// Declare some variables
a := 10
b := 3

// Arithmetic operators
fmt.Println(a + b)  // Output: 13
fmt.Println(a - b)  // Output: 7
fmt.Println(a * b)  // Output: 30
fmt.Println(a / b)  // Output: 3
fmt.Println(a % b)  // Output: 1
}``````

### Comparison Operators

Comparison operators are used to evaluate whether the values of two operands are equal or whether one value is greater than or less than the other. If the comparison is `true`, the result of the operator will be a `true` boolean value. The result will be a `false` boolean value if the comparison is `false`. Go has the following comparison operators:

Operator Description
`==` Equal to operator. Returns `true` if the value of operands are equal, `false` otherwise.
`!=` Not equal to operator. Returns `true` if the value of operands operands are not equal, `false` otherwise.
`>` Greater than operator. Returns `true` if the value of the first operand is greater than the second, `false` otherwise.
`<` Less than operator. Returns `true` if the value of the first operand is less than the second, `false` otherwise.
`>=` Greater than or equal to operator. Returns `true` if the value of the first operand is greater than or equal to the second, `false` otherwise.
`<=` Less than or equal to operator. Returns `true` if the value of the first operand is less than or equal to the second, `false` otherwise.

Here's an example of using Comparison Operators in Go:

Example:

``````package main
import "fmt"

func main() {
// Declare some variables
a := 10
b := 3

// Comparison operators
fmt.Println(a == b)  // Output: false
fmt.Println(a != b)  // Output: true
fmt.Println(a < b)   // Output: false
fmt.Println(a <= b)  // Output: false
fmt.Println(a > b)   // Output: true
fmt.Println(a >= b)  // Output: true
}
``````

### Assignment Operators

Go has several assignment operators that can be used to assign a value to a variable. These operators include:

Operator Description
`=` Simple assignment operator. Assigns the value on the right side to the variable on the left side
`+=` Addition assignment operator. Adds the value on the right side to the variable on the left side and assigns the result to the variable.
`-=` Subtraction assignment operator. Subtracts the value on the right side from the variable on the left side and assigns the result to the variable.
`*=` Multiplication assignment operator. Multiplies the value on the right side by the variable on the left side and assigns the result to the variable.
`/=` Division assignment operator. Divides the variable on the left side by the value on the right side and assigns the result to the variable.
`%=` Modulus assignment operator. Calculates the remainder when the variable on the left side is divided by the value on the right side and assigns the result to the variable.

Here's an example of using Assignment Operators in Go:

Example:

``````package main
import "fmt"

func main() {
// Declare some variables
a := 10
b := 3

// Assignment operators
e := a
fmt.Println(e)  // Output: 10
e += b
fmt.Println(e)  // Output: 13
e -= b
fmt.Println(e)  // Output: 10
e *= b
fmt.Println(e)  // Output: 30
e /= b
fmt.Println(e)  // Output: 10
e %= b
fmt.Println(e)  // Output: 1
}``````

### Logical Operators

Go has three logical operators that can be used to perform logical operations on boolean values. These operators include:

Operator Description
`&&` Logical AND operator. Returns `true` if both operands are `true``false` otherwise.
`||` Logical OR operator. Returns `true` if either operand is `true``false` otherwise.
`!` Logical NOT operator. Inverts the boolean value of the operand. If the operand is `true`, the NOT operator returns `false`. If the operand is `false`, the NOT operator returns `true`.

Here's an example of using Logical Operators in Go:

Example:

``````package main
import "fmt"

func main() {
// Declare some variables
c := true
d := false

// Logical operators
fmt.Println(c && d)  // Output: false
fmt.Println(c || d)  // Output: true
fmt.Println(!c)      // Output: false
}``````

### Bitwise Operators

Go also has several bitwise operators that can be used to perform bitwise operations on integer values. These operators include:

Operator Description
`&` Bitwise AND operator. Performs a bitwise AND operation on the two operands.
`|` Bitwise OR operator. Performs a bitwise OR operation on the two operands.
`^` Bitwise XOR operator. Performs a bitwise XOR operation on the two operands.
`&^` Bit clear operator. Performs a bit clear operation on the two operands.
`<<` Left shift operator. Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
`>>` Right shift operator. Shifts the bits of the left operand to the right by the number of positions specified by the right operand.

Here's an example of using Bitwise Operators in Go:

Example:

``````package main
import "fmt"

func main() {
a := 10
b := 3

// Bitwise AND
c := a & b
fmt.Println("a & b is", c) // Output: 2

// Bitwise OR
c = a | b
fmt.Println("a | b is", c) // Output: 1

// Bitwise XOR
c = a ^ b
fmt.Println("a ^ b is", c) // Output: 9

// Bit clear
c = a &^ b
fmt.Println("a &^ b is", c) // Output: 8

// Left shift
c = a << b
fmt.Println("a << b is", c) // Output: 80

// Right shift
c = a >> b
fmt.Println("a >> b is", c) // Output: 1
}``````

Go has an address operator, `&`, that can be used to obtain the memory address of a variable. The `&` operator generates a pointer to the variable.

Example:

``````package main
import "fmt"

func main() {
x := 10
fmt.Println(&x)
}
``````

The code above will print the memory address of the variable `x`.

### Pointer Operators

Go has a `*` operator that is used in conjunction with pointers. The `*` operator is used to dereference a pointer or access the value stored at the memory address pointed to by the pointer.

Example:

``````package main
import "fmt"

func main() {
x := 10
p := &x
fmt.Println(*p)
}
``````

The code above will print the value of `x`, which is `10`.