WEB DEVELOPER SITE

# Go - Recursion

Recursion is the process of repeating items in a self-similar way. The same concept applies in programming languages as well.

```func recursion() {
recursion() /* function calls itself */
}
func main() {
recursion()
}
```
The Go programming language supports recursion. That is, it allows a function to call itself.

## Examples of Recursion in Go

Recursive functions are very useful to solve many mathematical problems such as calculating factorial of a number, generating a Fibonacci series, etc.

### Example 1: Calculating Factorial Using Recursion in Go

The following example calculates the factorial of a given number using a recursive function
```package main

import "fmt"

func factorial(i int)int {
if(i <= 1) {
return 1
}
return i * factorial(i - 1)
}
func main() {
var i int = 15
fmt.Printf("Factorial of %d is %d", i, factorial(i))
}
```

## Example 2: Fibonacci Series Using Recursion in Go

```package main

import "fmt"

func fibonaci(i int) (ret int) {
if i == 0 {
return 0
}
if i == 1 {
return 1
}
return fibonaci(i-1) + fibonaci(i-2)
}
func main() {
var i int
for i = 0; i < 20; i++ {
fmt.Printf("%d ", fibonaci(i))
}
}
```
```\$go run main.go
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

```