Go – Arithmetic Operators

In this tutorial, we will learn about arithmetic operators in the Go programming language (Golang). Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and finding the remainder. We will explore these operators with examples and detailed explanations.


List of Arithmetic Operators

The following table lists all arithmetic operators in Go:

OperatorDescriptionExampleResult
+Additiona + bSum of a and b
-Subtractiona - bDifference of a and b
*Multiplicationa * bProduct of a and b
/Divisiona / bQuotient of a divided by b
%Modulusa % bRemainder when a is divided by b

Examples of Arithmetic Operators

Let’s see how to use these operators in a program with integer and floating-point numbers.


Example 1: Using Arithmetic Operators with Integers

Here’s a simple program demonstrating arithmetic operations with integers:

</>
Copy
package main

import "fmt"

func main() {
    a := 15
    b := 4

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

Explanation

  1. Declare Variables: Two integers, a and b, are initialized with the values 15 and 4, respectively.
  2. Perform Operations: Each arithmetic operation is performed, and the result is printed using fmt.Println.
  3. Division: Since a and b are integers, the division result is also an integer (truncated).
  4. Modulus: The modulus operator returns the remainder of the division.

Output


Example 2: Using Arithmetic Operators with Floating-Point Numbers

Let’s see how arithmetic operators work with floating-point numbers:

</>
Copy
package main

import "fmt"

func main() {
    x := 15.5
    y := 4.2

    fmt.Println("Addition:", x+y)
    fmt.Println("Subtraction:", x-y)
    fmt.Println("Multiplication:", x*y)
    fmt.Println("Division:", x/y)
}

Explanation

  1. Declare Variables: Two floating-point numbers, x and y, are initialized with the values 15.5 and 4.2, respectively.
  2. Perform Operations: Each arithmetic operation is performed, and the result is printed using fmt.Println.
  3. Division: For floating-point numbers, the division result includes the decimal part.

Output


Example 3: Using Arithmetic Operators in Expressions

Arithmetic operators can be combined to form complex expressions:

</>
Copy
package main

import "fmt"

func main() {
    a := 10
    b := 5
    c := 2

    result := (a + b) * c
    fmt.Println("Result of (a + b) * c:", result)
}

Explanation

  1. Declare Variables: Three integers, a, b, and c, are initialized with the values 10, 5, and 2, respectively.
  2. Form an Expression: The expression (a + b) * c is evaluated, where parentheses determine the order of operations.
  3. Print Result: The result of the expression is stored in result and printed.

Output


Key Notes on Arithmetic Operators

  • Division between integers returns an integer. To get a floating-point result, at least one operand must be a floating-point number.
  • The modulus operator (%) works only with integers.
  • Parentheses can be used to define the precedence of operations in complex expressions.

Conclusion

Arithmetic operators in Go are straightforward and essential for performing mathematical computations. Understanding how they work with integers and floating-point numbers will help you write efficient code. By combining operators in expressions, you can solve complex mathematical problems effectively.