Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Recursion

This Go program demonstrates a simple factorial function and a recursive Fibonacci function.

// Importing the "fmt" package, which provides functions for formatted I/O.
import "fmt"

// Function fact calculates the factorial of an integer using recursion.
func fact(n int) int {
    // Base case: factorial of 0 is 1.
    if n == 0 {
        return 1
    }

    // Recursive case: n! = n * (n-1)!
    return n * fact(n-1)
}

// The main function, which serves as the entry point for the program.
func main() {
    // Printing the factorial of 7 using the fact function.
    fmt.Println(fact(7))

    // Declaring a variable fib as a function type that takes an integer parameter and returns an integer.
    var fib func(n int) int

    // Assigning a recursive anonymous function to the fib variable.
    fib = func(n int) int {
        // Base cases: Fibonacci of 0 is 0, and Fibonacci of 1 is 1.
        if n < 2 {
            return n
        }

        // Recursive case: fib(n) = fib(n-1) + fib(n-2)
        return fib(n-1) + fib(n-2)
    }

    // Printing the 7th Fibonacci number using the fib function.
    fmt.Println(fib(7))
}

Output

040
13

Now, let's break down the code and explain each part:

  1. Function Declarations:

    • fact(n int) int: This function calculates the factorial of an integer n using recursion.

  2. Main Function:

    • main(): This is the entry point of the program.

    • fmt.Println(fact(7)): Calls the fact function and prints the factorial of 7.

    • Declares a variable fib as a function type that takes an integer parameter and returns an integer.

    • fib = func(n int) int { ... }: Assigns a recursive anonymous function to the fib variable.

    • fmt.Println(fib(7)): Calls the fib function and prints the 7th Fibonacci number.

PreviousClosureNextPointers

Last updated 1 year ago

Was this helpful?