 Go Part 3: Operators and Expressions in Go

# Go PART 3: OPERATORS AND EXPRESSIONS IN Go

## Operators and Expressions in Go

In this article, we’ll cover the most commonly used operators and expressions in Go, along with their syntax, examples, and outputs.

Go is a statically typed programming language that supports a wide range of operators and expressions. These operators and expressions allow you to perform various operations on variables, constants, and literals. In this article, we will explore the different types of operators and expressions in Go, and we’ll provide examples to demonstrate their usage.

## Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numerical values. The following table shows the arithmetic operators in Go:

Let’s take a look at some examples of arithmetic operators in Go:

``````package main

import "fmt"

func main() {
num1 := 10
num2 := 3

result := num1 + num2

// Subtraction
result = num1 - num2
fmt.Printf("Subtraction: %d\n", result)

// Multiplication
result = num1 * num2
fmt.Printf("Multiplication: %d\n", result)

// Division
result = num1 / num2
fmt.Printf("Division: %d\n", result)

// Modulus
result = num1 % num2
fmt.Printf("Modulus: %d\n", result)
}
``````

### Output

```Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
```

## Comparison Operators

Comparison operators are used to compare values. The following table shows the comparison operators in Go:

Here’s an example Go program that uses the comparison operators:

``````package main

import "fmt"

func main() {
num1 := 5
num2 := 3

fmt.Printf("Is %d equal to %d? %t\n", num1, num2, num1 == num2)
fmt.Printf("Is %d not equal to %d? %t\n", num1, num2, num1 != num2)
fmt.Printf("Is %d greater than %d? %t\n", num1, num2, num1 > num2)
fmt.Printf("Is %d less than %d? %t\n", num1, num2, num1 < num2)
fmt.Printf("Is %d greater than or equal to %d? %t\n", num1, num2, num1 >= num2)
fmt.Printf("Is %d less than or equal to %d? %t\n", num1, num2, num1 <= num2)
}
``````

### Output

```Is 5 equal to 3? false
Is 5 not equal to 3? true
Is 5 greater than 3? true
Is 5 less than 3? false
Is 5 greater than or equal to 3? true
Is 5 less than or equal to 3? false
```

## Logical Operators

Logical operators are used to combine multiple expressions and evaluate them as a single Boolean expression. The following table shows the Logical operators in Go:

Here’s an example program to demonstrate the logical operators:

``````package main

import "fmt"

func main() {
x := 10
y := 5
z := 8

// logical AND operator
if x > y && x < z {
fmt.Println("x is between y and z")
} else {
fmt.Println("x is not between y and z")
}

// logical OR operator
if x < y || x > z {
fmt.Println("x is either less than y or greater than z")
} else {
fmt.Println("x is between y and z")
}

// logical NOT operator
if !(x == y) {
fmt.Println("x is not equal to y")
} else {
fmt.Println("x is equal to y")
}
}

``````

### Output

```x is not between y and z
x is either less than y or greater than z
x is not equal to y
```

## Conclusion

In conclusion, operators and expressions are an essential part of any programming language, including Go. Operators are symbols or keywords that perform an action on one or more operands to produce a result. There are several types of operators in Go, including arithmetic, comparison, and logical operators, each with its own set of rules and precedence.

Go Beginner Tutorial Series