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).
|
0 1 2 3 4 5 6 7 8 9 |
fmt.Println(17 / 5) // 3 fmt.Println(-17 / 5) // -3 ← not -4 fmt.Println(17 / -5) // -3 fmt.Println(-17 / -5) // 3 |
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.
|
0 1 2 3 4 5 6 7 8 9 |
fmt.Println(17 % 5) // 2 because 5*3 + 2 = 17 fmt.Println(-17 % 5) // -2 because 5*(-3) + (-2) = -17 fmt.Println(17 % -5) // 2 because -5*(-3) + 2 = 17 fmt.Println(-17 % -5) // -2 |
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:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package main import "fmt" func main() { a, b := 17, 5 negA, negB := -17, -5 fmt.Println("Basic arithmetic:") fmt.Printf("%d + %d = %d\n", a, b, a+b) // 22 fmt.Printf("%d - %d = %d\n", a, b, a-b) // 12 fmt.Printf("%d * %d = %d\n", a, b, a*b) // 85 fmt.Printf("%d / %d = %d (trunc toward zero)\n", a, b, a/b) // 3 fmt.Printf("%d %% %d = %d\n", a, b, a%b) // 2 fmt.Println("\nWith negatives:") fmt.Printf("-17 / 5 = %d ← NOT -4 !\n", negA/b) // -3 fmt.Printf("-17 %% 5 = %d ← sign follows left\n", negA%b) // -2 fmt.Printf("17 %% -5 = %d\n", a/negB) // -3 fmt.Printf("-17 / -5 = %d\n", negA/negB) // 3 fmt.Println("\nFloating point (always float64 by default):") f1, f2 := 17.0, 5.0 fmt.Printf("%.2f / %.2f = %.2f\n", f1, f2, f1/f2) // 3.40 fmt.Println("\nString concatenation with + :") fmt.Println("Hello" + " from" + " Hyderabad!" + " 🇮🇳") } |
5. String Concatenation — Special Case of +
|
0 1 2 3 4 5 6 7 8 9 10 11 |
greeting := "Namaste" place := "Hyderabad" emoji := "🇮🇳" full := greeting + ", " + place + "! " + emoji fmt.Println(full) // Namaste, Hyderabad! 🇮🇳 |
Important notes:
- Only + and += work for strings
- No * repeat like Python “hi” * 3
- For heavy concatenation → use strings.Builder (much faster)
|
0 1 2 3 4 5 6 7 8 9 |
var sb strings.Builder sb.WriteString("User: ") sb.WriteString("Webliance") result := sb.String() |
6. Common Beginner Mistakes & How to Avoid Them
- Expecting floor division with negatives → Always remember: truncates toward zero → If you need floor division → write your own helper or use math.Floor
- Doing a / b with integers when you want decimal → Force at least one operand to float: float64(a) / float64(b)
- Using % with floats → compile error — % only for integers
- 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:
- 23 / 7 → ?
- -23 / 7 → ?
- -23 % 7 → ?
- 23 % -7 → ?
- “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! 💪🇮🇳🚀
