# Go by Example: Recursion

Recursion is a technique in computer programming where a function calls itself to solve a problem. In Go, recursion can be used to implement functions that perform repetitive operations, like finding the factorial of a number or the nth Fibonacci number.

Here’s an example that calculates the factorial of a number using recursion:

``````package main

import "fmt"

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

func main() {
fmt.Println("factorial of 5:", factorial(5))
}``````

Output:

``factorial of 5: 120``

In this example, the `factorial` function calculates the factorial of a number `n` by recursively calling itself. The base case is when `n` is equal to 0, in which case the function returns `1`. In all other cases, the function returns `n` multiplied by the result of calling itself with `n-1`. This ensures that the function continues to call itself until it reaches the base case, at which point it returns the final result.

Here’s another example that calculates the nth Fibonacci number using recursion:

``````package main

import "fmt"

func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
}

func main() {
fmt.Println("6th Fibonacci number:", fibonacci(6))
}``````

Output:

``6th Fibonacci number: 8``

In this example, the `fibonacci` function calculates the nth Fibonacci number by recursively calling itself. The base case is when `n` is less than or equal to `1`, in which case the function returns `n`. In all other cases, the function returns the sum of the results of calling itself with `n-1` and `n-2`. This ensures that the function continues to call itself until it reaches the base case, at which point it returns the final result.

When writing recursive functions, it is important to make sure that the base case is clearly defined and that the recursive calls eventually lead to the base case. If the base case is not reached, the function will continue to call itself indefinitely, resulting in an infinite loop and a stack overflow error.

It’s also important to consider the performance of recursive functions. Some recursive algorithms have a large time complexity, especially when the problem size is large, due to the overhead of repeatedly calling the function and building up the call stack. In such cases, iterative solutions may be more appropriate.

Additionally, recursive solutions may be less memory efficient than iterative solutions, as each call to the function adds another level to the call stack and requires additional memory.

However, in certain cases, recursion can be a simple and elegant solution to problems that are naturally recursive in nature. It is also an important technique to understand for interview questions and programming competitions.

In conclusion, recursion is a powerful technique in computer programming, but it’s important to use it judiciously and to consider the performance implications of recursive solutions when solving problems.