Recursion

A recursive function is a function that call itself unless it reaches matches a certain condition.

We are going to implement a classic example to calculate factorial or given number using Go, with recursive methodology

package main
import "fmt"
func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n-1)
}
func main() {
    fmt.Println(factorial(5))
}
// 120

Last updated

Was this helpful?