Go – Factorial Program using Recursion

In recursion, the function calls itself.

To find factorial of a given number in Go language, we can write a function factorial, as shown below, which is a recursive function.

example.go

package main

import "fmt"

func factorial(n int) int {
	if n == 0 {
		return 1
	}
	return n * factorial(n-1)
}

func main() {
	n := 5
	result := factorial(n)
	fmt.Println("Factorial of", n, "is :", result)
}

Output

Factorial of 5 is : 120

Conclusion

In this Golang Tutorial, we learned how to write a Go program to compute factorial of a number using recursion technique.