Go (Golang) Operators with Examples

In this tutorial, we will learn how to work with Operators in Golang with examples.

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

Table of contents:

In this tutorial, we will learn the following topics with examples:
  • Go - arithmetic operators
  • Go-  sign operators
  • Go - assignment operator
  • Go - increment and decrement operators
  • Go - Boolean operators
  • Go - Logical and (&&) operator
  • Go - Logical or (||) operator
  • Go - negation operator !
  • Go - bitwise operators
  • Go - comparison operators

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 - 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 - 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

Go - bitwise operators

Try the following example to understand all the bitwise operators available in the Go programming language −


package main

import "fmt"

func main() {
   var a uint = 60	/* 60 = 0011 1100 */  
   var b uint = 13	/* 13 = 0000 1101 */
   var c uint = 0          

   c = a & b       /* 12 = 0000 1100 */ 
   fmt.Printf("Line 1 - Value of c is %d\n", c )

   c = a | b       /* 61 = 0011 1101 */
   fmt.Printf("Line 2 - Value of c is %d\n", c )

   c = a ^ b       /* 49 = 0011 0001 */
   fmt.Printf("Line 3 - Value of c is %d\n", c )

   c = a << 2     /* 240 = 1111 0000 */
   fmt.Printf("Line 4 - Value of c is %d\n", c )

   c = a >> 2     /* 15 = 0000 1111 */
   fmt.Printf("Line 5 - Value of c is %d\n", c )
}

Output:

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is 240
Line 5 - Value of c is 15

Go - comparison operators

In the code example, we have four expressions. These expressions compare integer values.


package main

import "fmt"

func main() {

    fmt.Println(2 < 3)
    fmt.Println(3 == 4)
    fmt.Println(3 >= 3)
    fmt.Println(4 != 3)
}

Output:

true
false
true
true

Comments