go-examples

Get Set Learn Go


Project maintained by sagar-jadhav Hosted on GitHub Pages — Theme by mattgraham

Recursion

Example demonstrates how to use recursion in the Go programming language. Recursion is a function calling itself. Recursion is a key aspect in functional programming.

package main

import "fmt"

func main() {
	fmt.Println(calculateFactorial(5))
}

func calculateFactorial(val int) int {
	if val == 0 {
		return 1
	}
	return val * calculateFactorial(val-1)
}

Output

120
Try It Out Source Code

Contributors

« Home Page Previous « Closure Next » Boolean Expressions