← All Exercises

Go Recursive Factorial

intermediatego~15min

Background

Recursion in Go has no tail-call optimization, making deep recursion risky for large inputs.

Problem

Write a Go function that computes factorial recursively, returning an error for negative inputs or potential overflow.

Input

A non-negative integer n.

Output

The factorial of n, or an error for invalid input.

Sample tests

Sample 1

Input: 5
Expected: 120, nil

Your solution

Learning objective

Learn recursion in Go, error propagation through recursive calls, and stack limitations.