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:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | a + b | Sum of a and b |
- | Subtraction | a - b | Difference of a and b |
* | Multiplication | a * b | Product of a and b |
/ | Division | a / b | Quotient of a divided by b |
% | Modulus | a % b | Remainder 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:
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
- Declare Variables: Two integers,
a
andb
, are initialized with the values 15 and 4, respectively. - Perform Operations: Each arithmetic operation is performed, and the result is printed using
fmt.Println
. - Division: Since
a
andb
are integers, the division result is also an integer (truncated). - 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:
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
- Declare Variables: Two floating-point numbers,
x
andy
, are initialized with the values 15.5 and 4.2, respectively. - Perform Operations: Each arithmetic operation is performed, and the result is printed using
fmt.Println
. - 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:
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
- Declare Variables: Three integers,
a
,b
, andc
, are initialized with the values 10, 5, and 2, respectively. - Form an Expression: The expression
(a + b) * c
is evaluated, where parentheses determine the order of operations. - 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.