Go Lang - A Beginner's Guide to Operators: Arithmetic, Comparison, and Logical
Tags: go, tutorial, programming, codenewbie
Published: 2023-03-27 09:41:00

Welcome to our Go Lang tutorial series, where we aim to introduce beginners to the world of Go programming. In this blog post, we will dive into the different types of operators in Go - arithmetic, comparison, and logical operators. We will provide detailed explanations, visual aids, and code examples to make it as easy as possible for you to understand and start using these operators in your Go programs.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qp3g2h9i2ou4r4jdvb0n.png)

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. In Go, there are five main arithmetic operators:

- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)

Let's look at some code examples using these operators:

```
package main

import "fmt"

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

    sum := a + b
    difference := a - b
    product := a * b
    quotient := a / b
    remainder := a % b

    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", difference)
    fmt.Println("Product:", product)
    fmt.Println("Quotient:", quotient)
    fmt.Println("Remainder:", remainder)
}

```

Output:

```
Sum: 13
Difference: 7
Product: 30
Quotient: 3
Remainder: 1

```

1. Comparison Operators

Comparison operators are used to compare two values and return a boolean result (true or false) based on the comparison. Go provides six comparison operators:

- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)

Here's a code example demonstrating the use of comparison operators:

```
package main

import "fmt"

func main() {
    x := 42
    y := 35

    fmt.Println("x == y:", x == y)
    fmt.Println("x != y:", x != y)
    fmt.Println("x > y:", x > y)
    fmt.Println("x < y:", x < y)
    fmt.Println("x >= y:", x >= y)
    fmt.Println("x <= y:", x <= y)
}

```

Output:

```

x == y: false
x != y: true
x > y: true
x < y: false
x >= y: true
x <= y: false

```

1. Logical Operators

Logical operators are used to evaluate boolean expressions, which are expressions that can only be true or false. Go has three logical operators:

- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)

Let's see these operators in action with a code example:

```
package main

import "fmt"

func main() {
    a := true
    b := false

    fmt.Println("a && b:", a && b)
    fmt.Println("a || b:", a || b)
    fmt.Println("!a:", !a)
    fmt.Println("!b:", !b)
}

```

Output:

```

a && b: false
a || b: true
!a: false
!b: true

```

Conclusion:

Now you have a good understanding of the arithmetic, comparison, and logical operators in Go. These are fundamental building blocks of any programming language and mastering them is essential for writing efficient and readable code. In the next tutorial, we will explore "Control Flow" in Go, including conditional statements, loops, and flow control mechanisms. Stay tuned to learn more about these essential concepts that will help you write more dynamic and powerful Go programs.