×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Go Pointers

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Go Recursion Functions

Last Updated : July 21, 2025

In Go, recursion is a technique where a function calls itself to solve smaller instances of a problem until it reaches a base case. It is commonly used in problems like factorial, Fibonacci sequence, and tree traversal.

What is Recursion?

A recursive function is one that calls itself. Every recursive function must have:

  • A base case that stops the recursion.
  • A recursive case that breaks the problem into smaller subproblems.

Recursive Function in Go

A recursive function is a function that calls itself to solve a smaller instance of the same problem until a base condition is met.

Syntax

The basic syntax of a recursive function includes a base condition to stop recursion and a recursive call with a smaller input:

func functionName(params) returnType {
    if baseCondition {
        return baseResult
    }
    return functionName(smallerInput)
}

Example: Factorial using Recursion

The following example demonstrates a recursive function in Go that calculates the factorial of a given number:

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 is:", factorial(5))
}

When you run the above code, the output will be:

Factorial of 5 is: 120

Example: Fibonacci Series using Recursion

The following example shows a recursive function to generate Fibonacci numbers, where each number is the sum of the two preceding ones:

package main
import "fmt"

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

func main() {
    for i := 0; i < 6; i++ {
        fmt.Printf("%d ", fibonacci(i))
    }
}

When you run the above code, the output will be:

0 1 1 2 3 5

Pros and Cons of Recursion

  • Pros: The recursion simplifies complex problems like tree traversal or mathematical sequences.
  • Cons: The recursion can lead to high memory usage and stack overflow if not used carefully.

Tail Recursion

Go does not optimize tail recursion. So even if a recursive call is the last statement, it won’t be optimized to an iterative call.

Example

The following example demonstrates a recursive function that performs a countdown, printing numbers in descending order until it reaches zero:

package main
import "fmt"

func countdown(n int) {
    if n == 0 {
        fmt.Println("Done!")
        return
    }
    fmt.Println(n)
    countdown(n - 1)
}

func main() {
    countdown(5)
}

When you run the above code, the output will be:

5
4
3
2
1
Done!

Go Recursion Functions Exercise

Test your understanding of recursion in Go.

  1. What is a necessary part of a recursive function?
  2. What happens if the base case is missing in recursion?
  3. Which of these is a valid example of recursion?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.