Go Operators
Estimated reading: 3 minutes 43 views

➕ Go Arithmetic Operators – Perform Math Operations in Go (2025 Guide)

🧲 Introduction – Why Arithmetic Operators Matter in Go

Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulo. Whether you’re building a calculator, processing data, or performing logic in backend systems, mastering Go’s arithmetic operators is foundational.

🎯 In this section, you’ll learn:

  • The full list of arithmetic operators in Go
  • Syntax rules with examples and outputs
  • How Go handles integer vs float division
  • Real-world usage and operator precedence

➗ List of Arithmetic Operators in Go

OperatorSymbolDescriptionExampleOutput
Addition+Adds two values5 + 38
Subtract-Subtracts right from left10 - 46
Multiply*Multiplies values6 * 742
Divide/Divides left by right20 / 54
Modulo%Returns remainder10 % 31

🔢 Syntax – Using Arithmetic Operators in Go

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("Modulo:", a%b)
}

📤 Output:

Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3
Modulo: 3

🧠 Note: Division between two integers always produces an integer result (truncated).


🎯 Float Division Example

x := 10.0
y := 4.0
fmt.Println(x / y)  // Output: 2.5

If either operand is a float, the result will be a floating-point value.


🚫 Type Mismatch: Must Match Types

You cannot mix int and float64 directly:

var a int = 5
var b float64 = 2.5
// fmt.Println(a + b) ❌ Compile-time error

✅ Fix:

fmt.Println(float64(a) + b)

🧪 Example – Arithmetic Expression

a, b, c := 10, 3, 2
result := a + b*c - a/b
fmt.Println(result)

🧮 Breakdown:

a + (b * c) - (a / b)
10 + 6 - 3 = 13

📤 Output:

13

🧠 Operator Precedence in Go

Precedence (High → Low)Operators
1 (Highest)*, /, %
2+, -

Use parentheses () to override precedence for complex expressions.


📌 Summary – Recap & Next Steps

Go’s arithmetic operators are straightforward but require attention to types and division behavior. These operators are frequently used in algorithms, loops, and conditions.

🔍 Key Takeaways:

  • Use +, -, *, /, % for basic arithmetic
  • Integer division discards the decimal part
  • Use float64 for precise division
  • Match operand types before performing operations
  • Use parentheses to control order of operations

⚙️ Next: Explore Relational Operators in Go to compare values using ==, !=, <, > and more.


❓ FAQs – Arithmetic Operators in Go

❓ What happens when I divide two integers in Go?
✅ Go returns an integer result. The decimal part is truncated.

❓ How do I perform floating-point division?
✅ Use float32 or float64 operands:

fmt.Println(10.0 / 4.0) // Output: 2.5

❓ Can I use arithmetic operators on strings or booleans?
✅ No. Arithmetic operators only work with numeric types (int, float, complex).

❓ What does % (modulo) return?
✅ It returns the remainder of an integer division: 10 % 3 = 1

❓ How can I change precedence in an expression?
✅ Use parentheses to group operations:

result := (a + b) * c

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Go Arithmetic Operators

Or Copy Link

CONTENTS
Scroll to Top