Go does not support tail call optimization, so tail-recursive functions will consume stack space and may overflow.
Go does not perform tail call optimization (TCO), meaning tail-recursive functions will grow the call stack and eventually cause a stack overflow. Unlike languages like Scheme or Scala, the Go compiler intentionally omits TCO to preserve stack traces for debugging and to simplify the runtime model.
func factorial(n int) int {
if n <= 1 {
return 1
}
// This is a tail call, but Go will NOT optimize it.
// Deep recursion here will panic with "runtime: stack overflow".
return factorial(n-1) * n
}
Tail call optimization is a feature where a function call at the very end of another function reuses the current stack space instead of creating a new one. Go does not support this, so recursive functions that call themselves as their last action will eventually run out of memory if the recursion is too deep. Think of it like a stack of plates: in other languages, the top plate gets reused; in Go, a new plate is added every time, eventually making the stack too tall to hold.