# Go Operators Tutorial

In this Go tutorial we learn about symbols that have special meaning to the compiler, called operators.

We introduce arithmetic, assignment, comparison (relational), and logical (conditional) operators. We also take a quick look at operator precedence.

Here's a table of contents of what you'll learn in this lesson:

(*click on a link to skip to its section*)

Let's jump right in.

## What are operators

Operators are one or more symbols in Go that have a special meaning to the compiler, allowing us to perform various operations within our application.

As an example, let’s consider the + symbol. The + symbol is an operator to perform arithmetic on two or more numeric values. But, when used on two or more strings, the + operator will combine (concatenate) them into a single string.

The following types of operators are available in Go.

- Arithmetic
- Assignment
- Bitwise
- Comparison (Relational)
- Logical (Conditional)

*Note that you don’t need to memorize these operators. We will be using them throughout the course, which will allow you to get used to them easily*.

## Arithmetic operators

Go supports the following the arithmetic operators.

Operator | Description | Example |
---|---|---|

+ | Adds operands | 5 + 3 = 8 |

- | Subtract the second operand from the first | 5 - 3 = 2 |

* | Multiplies the first operand with the second | 5 * 3 = 15 |

/ | Divide the first operand with the second | 5 / 3 = 1 |

% | Modulus. Returns the remainder of a division | 5 % 3 = 2 |

++ | Increment. Increase an interger value by one | 5++ = 6 |

-- | Decrement. Decrease an integer value by one | 5-- = 4 |

## Assignment operators

Go supports the following assignment operators.

Operator | Description |
---|---|

= | Assignment. Assign right operand to the left operand |

:= | Dynamic Assignment. Assign right operand to the left operand without specifying a type |

+= | Add and assign. Add the right operand to the left operand and assign the result to the left operand |

-= | Subtract and assign. Subtract the right operand from the left operand and assign the result to the left operand |

*= | Multiply and assign. Multiply the left operand with the right operand and assign the result to the left operand |

/= | Divide and assign. Divide the left operand with the right operand and assign the result to the left operand |

%= | Modulus and assign. Divide the left operand with the right operand and assign the remainder to the left operand |

**Example:**

```
package main
import "fmt"
func main() {
// =
var num1 int = 5
fmt.Println("num1:", num1)
// :=
num2 := 3
fmt.Println("num2:", num2)
// +=
num1 += num2
fmt.Println("num1 += num2:", num1)
// -=
num1 -= num2
fmt.Println("num1 -= num2:", num1)
// *=
num1 *= num2
fmt.Println("num1 *= num2:", num1)
// /=
num1 /= num2
fmt.Println("num1 /= num2:", num1)
// %=
num1 %= num2
fmt.Println("num1 %= num2: ", num1)
}
```

## Comparison (Relational) operators

Go supports the following comparison operators.

Operator | Description |
---|---|

== | Evaluates if the values of two operands are equal |

!= | Evaluates if the values of two operands are not equal |

> | Evaluates if the value of the left operand is greater than the value of the right operand |

< | Evaluates if the value of the left operand is less than the value of the right operand |

>= | Evaluates if the value of the left operand is greater than or equal to the value of the right operand |

<= | Evaluates if the value of the left operand is less than or equal to the value of the right operand |

**Example:**

```
package main
import "fmt"
func main() {
var result bool = false
// ==
result = 1 == 1
fmt.Println("1 == 1:", result)
// !=
result = 1 != 2
fmt.Println("1 != 2:", result)
// >
result = 1 > 2
fmt.Println("1 > 2:", result)
// <
result = 1 < 2
fmt.Println("1 < 2:", result)
// >=
result = 1 >= 1
fmt.Println("1 >= 1:", result)
// <=
result = 2 <= 1
fmt.Println("2 <= 1:", result)
}
```

## Logical (Conditional) operators

Go supports the following comparison operators.

Operator | Name | Description |
---|---|---|

&& | Conditional AND operator | If both operands are non-zero, the result is true |

|| | Conditional OR operator | If one of the two operands is non-zero, the result is true |

! | Conditional NOT operator | If the condition is not true, the result becomes true |

**Example:**

```
package main
import "fmt"
func main() {
var op1 bool = true
var op2 bool = false
// &&
fmt.Println("op1 AND op2 = true:", op1 && op2)
// ||
fmt.Println("op1 OR op2 = true:", op1 || op2)
// !
fmt.Println("op1 = NOT true:", !op1)
// !
fmt.Println("op2 = NOT true:", !op2)
}
```

## Operator precedence

Certain operators in Go will have higher precedence than others.

As an example, let’s consider the precedence of the multiplication and addition operators.

**Example:**

` 5 + 3 * 2`

If we read the calculation from left to right, we would do the addition first.

But, the multiplication operator has a higher precedence than the addition operator, so it will do that part of the calculation first.

**Example:**

```
// correct
3 * 2 = 6
5 + 6 = 11
// incorrect
5 + 3 = 8
8 * 2 = 16
```

We can change the operator precedence by wrapping operators in parentheses.

**Example:**

` (5 + 3) * 2`

In the example above, we want the addition to be performed before the multiplication, so we wrap the addition in parentheses.

**Example:**

```
(5 + 3) = 8
8 * 2 = 16
```

## Summary: Points to remember

- Operators are symbols with a special meaning in Go.
- Go supports arithmetic, assignment, comparison (relational), logical (comparison) and bitwise operators.
- Some operators in Go will have a higher precedence than others.
- We can change precedence in some situations by wrapping operators in parentheses.