Go operator examples

In this example, we will learn how use different operators in Go with examples.

An operator is a special symbol which indicates a certain process is carried out.

Go sign operators

The + and - signs indicate the sign of a value. The plus sign can be used to signal that we have a positive number.


package main

import "fmt"

func main() {

    fmt.Println(2)
    fmt.Println(+2)
    fmt.Println(-2)
}

Output:

2
2
-2

Go assignment operator

The assignment operator = assigns a value to a variable. A variable is a placeholder for a value.


package main

import "fmt"

func main() {
    var x = 2
    fmt.Println(x)
}

Output:

2
-2

Go has a short variable declaration operator :=; it declares a variable and assigns a value in one step. The x := 2 is equal to var x = 2.


package main

import "fmt"

func main() {
    x := 2
    fmt.Println(x)
}

Output:

2
-2

Go increment and decrement operators

We often increment or decrement a value by one in programming. Go has two convenient operators for this: ++ and --.


package main

import "fmt"

func main() {

    x := 5

    x++
    x++

    fmt.Println(x)

    x--
    fmt.Println(x)
}

Output:

7
6

Go arithmetic operators

The following example shows arithmetic operations.


package main

import "fmt"

func main() {

    var a = 2
    var b = 3
    var c = 4

    var add = a + b + c
    var sb = c - a
    var mult = a * b
    var div = c / 2
    var rem = c % a

    fmt.Println(add)
    fmt.Println(sb)
    fmt.Println(mult)
    fmt.Println(div)
    fmt.Println(rem)
}

Output:

9
2
6
2
0

In the above example, we use addition, subtraction, multiplication, division, and remainder operations.

Go Boolean operators

Boolean operators are also called logical.

Many expressions result in a boolean value. For instance, boolean values are used in conditional statements.


package main

import "fmt"

func main() {

    var x = 3
    var y = 5

    fmt.Println(x == y)
    fmt.Println(y > x)

    if y > x {

        fmt.Println("y is greater than x")
    }
}

Output:

false
true
y is greater than x

Go Logical and (&&) operator

The code example shows the logical and (&&) operator. It evaluates to true only if both operands are true.


package main

import "fmt"

func main() {

    var a = true && true
    var b = true && false
    var c = false && true
    var d = false && false

    fmt.Println(a)
    fmt.Println(b)
    fmt.Println(c)
    fmt.Println(d)
}

Output:

true
false
false
false

Go Logical or (||) operator

The logical or (||) operator evaluates to true if either of the operands is true.


package main

import "fmt"

func main() {

    var a = true || true
    var b = true || false
    var c = false || true
    var d = false || false

    fmt.Println(a)
    fmt.Println(b)
    fmt.Println(c)
    fmt.Println(d)
}

Output:

true
true
true
false

Go negation operator !

The negation operator ! makes true false and false true.


package main

import "fmt"

func main() {

    fmt.Println(!true)
    fmt.Println(!false)
}

Output:

false
true
true