Chapter 26: Arithmetic

Arithmetic operators.

These are the classic math symbols you already know from school (+ – * / %), but Go has some very specific behaviors that catch almost every beginner off-guard at least once — especially integer division, remainder with negative numbers, and the fact that string concatenation uses + too.

Let’s go through everything about arithmetic operators in Go like we’re sitting together with VS Code open, chai in hand, typing and printing results live.

1. The Five Arithmetic Operators in Go

Operator Name Works on Example expression Typical result Important notes / gotchas
+ Addition numbers, strings 5 + 3 “Hi” + “!” 8 “Hi!” string concatenation only with + (no += for strings in older style)
Subtraction numbers 17 – 5 12
* Multiplication numbers 4 * 7 28
/ Division numbers 17 / 5 17.0 / 5 3 3.4 integer division truncates toward zero
% Remainder (modulo) integers only 17 % 5 -17 % 5 2 -2 sign of result follows dividend (not divisor)

2. Most Important Rule — Integer Division Truncates Toward Zero

This is different from some languages (Python 3 floors, C99 rounds toward zero — Go follows C99 style).

Go

Many beginners expect floor division (like Python //):

  • 17 / 5 = 3.4 → floor would be 3 → correct
  • -17 / 5 = -3.4 → floor would be -4 → but Go gives -3

How to remember: Go always throws away the fractional part toward zero.

3. Remainder % — Sign Follows the Dividend (Left Side)

This is another classic source of surprise.

Go

Rule to memorize:

The sign of a % b is the same as the sign of a (the dividend / left operand)

4. Full Runnable Example — Try This Right Now

Create arithmetic_operators.go:

Go

5. String Concatenation — Special Case of +

Go

Important notes:

  • Only + and += work for strings
  • No * repeat like Python “hi” * 3
  • For heavy concatenation → use strings.Builder (much faster)
Go

6. Common Beginner Mistakes & How to Avoid Them

  1. Expecting floor division with negatives → Always remember: truncates toward zero → If you need floor division → write your own helper or use math.Floor
  2. Doing a / b with integers when you want decimal → Force at least one operand to float: float64(a) / float64(b)
  3. Using % with floats → compile error — % only for integers
  4. Forgetting that string + creates new strings (can be slow in loops) → Use strings.Builder or fmt.Sprintf for heavy work

7. Quick Practice Questions for You

Try to predict the output before running:

  1. 23 / 7 → ?
  2. -23 / 7 → ?
  3. -23 % 7 → ?
  4. 23 % -7 → ?
  5. “Go” + ” ” + “Rocks!” → ?

Then run them — did any surprise you?

Any part still confusing?

  • Why Go chose truncate-toward-zero instead of floor?
  • How to implement true modulo (positive remainder)?
  • String concatenation performance in loops?
  • Or ready for comparison operators or bitwise next?

Keep typing small expressions and printing results — arithmetic is used in almost every real program, so feeling confident here will help you a lot.

You’re doing fantastic — keep going! 💪🇮🇳🚀

You may also like...

Leave a Reply

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