Go: panic: runtime error: integer divide by zero
Integer division or modulo by zero.
panic: runtime error: integer divide by zerointeger divide by zeroruntime error: integer divide by zero goroutine Meaning
Floating-point division by zero yields Inf/NaN, but integer division panics. Validate the divisor, especially values from user input, lengths, or counters (total / count).
Common causes
- Divisor derived from len() of an empty slice
- User input zero
- Counter not incremented
- Uninitialised struct field used as divisor
- Modulo by zero
⚡ Quick fix
- Check
if d == 0 { return error } - Use float division if Inf is acceptable
- Validate input at boundaries
Detailed fix by platform
Go
if count == 0 { return 0, errors.New("empty") }; avg := sum / count
How to diagnose
- Divisor — Zero when?
- Source — Input or length?
- Guard — Missing?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- fatal error: stack overflow Go: runtime: goroutine stack exceeds 1000000000-byte limit — fatal error: stack overflow
- invalid memory address or nil pointer dereference Go: panic: runtime error: invalid memory address or nil pointer dereference
- panic: assignment to entry in nil map Go: panic: assignment to entry in nil map
Most viewed in Go
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 4 Nov 2026