GoLang - Operators

2 minute read

In this tutorial, you will learn about Operators in Go Programming with the help of example.

Operators in Go programming are special symbols or keywords that perform various operations on operands (values or variables). They allow you to manipulate data, perform mathematical calculations, compare values, and much more. In this tutorial, we will explore the different types of operators in Go programming, along with examples, using simple and easy-to-understand language. So let’s dive in and learn about operators in Go!

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical calculations such as addition, subtraction, multiplication, division, and more.

Example:

package main

import "fmt"

func main() {
    var a = 10
    var b = 5

    fmt.Println("Addition:", a+b)
    fmt.Println("Subtraction:", a-b)
    fmt.Println("Multiplication:", a*b)
    fmt.Println("Division:", a/b)
    fmt.Println("Modulo:", a%b)
    fmt.Println("Increment:", a+1)
    fmt.Println("Decrement:", a-1)
}

In the example above, we declare two variables, a and b, with initial values. We then use arithmetic operators to perform addition, subtraction, multiplication, division, modulo (remainder), increment, and decrement operations on these variables.

Assignment Operators

Assignment operators are used to assign values to variables.

Example:

package main

import "fmt"

func main() {
    var a = 10

    a += 5
    fmt.Println("Add and assign:", a)

    a -= 3
    fmt.Println("Subtract and assign:", a)

    a *= 2
    fmt.Println("Multiply and assign:", a)

    a /= 4
    fmt.Println("Divide and assign:", a)

    a %= 3
    fmt.Println("Modulo and assign:", a)
}

In this example, we declare 'a' variable 'a' with an initial value. We then use assignment operators to modify the value of a by performing addition, subtraction, multiplication, division, and modulo operations.

Comparison Operators

Comparison operators are used to compare values and return a Boolean result (true or false).

Example:

package main

import "fmt"

func main() {
    var a = 10
    var b = 5

    fmt.Println("Equal to:", a == b)
    fmt.Println("Not equal to:", a != b)
    fmt.Println("Greater than:", a > b)
    fmt.Println("Less than:", a < b)
    fmt.Println("Greater than or equal to:", a >= b)
    fmt.Println("Less than or equal to:", a <= b)
}

In this example, we compare the values of variables 'a' and 'b' using comparison operators. The result of each comparison is printed, indicating whether the condition is true or false.

Logical Operators

Logical operators are used to combine multiple conditions and return a Boolean result.

Example:

package main

import "fmt"

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

    fmt.Println("Logical AND:", a && b)
    fmt.Println("Logical OR:", a || b)
    fmt.Println("Logical NOT:", !a)
}

In this example, we declare two Boolean variables, a and b, with initial values. We then use logical operators to perform logical AND, logical OR, and logical NOT operations on these variables.

Conclusion

Operators in Go programming are powerful tools that allow you to perform a wide range of operations on your data. In this tutorial, we explored arithmetic operators for mathematical calculations, assignment!

Updated: