Code with Go
Ask or search…
K
Links

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. 1.
    Function Declarations:
    • fact(n int) int: This function calculates the factorial of an integer n using recursion.
  2. 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.
Last modified 1mo ago