Gaaker Go

Golang Arithmetic operations

Common binary operators that can be used for integer and floats are +, -, *, and /.

/ For integer operations, the result is still an integer, for example:

9 / 4 = 2

The remainder operator (%) can only work on integers.

9 % 4

Operators have precedence of operations. Calculate + and - first and then *, % and /.

If there are parentheses, the calculations within the parentheses will be evaluated first.

The following is a complete code example.

package main

import "fmt"

func main() {
    fmt.Println(1 + 1 - 1.1)

    fmt.Println(3 * 2)

    fmt.Println(9/4)

    fmt.Println(9%4)

    fmt.Println(1 - (1 + 2) * 3)
}

Below is the program output:

0.9
6
2
1
-8
< Arithmetic operations Arithmetic operations >