Search⌘ K
AI Features

Solution Review: Finding Fibonacci Numbers with Array

Explore how to implement Fibonacci number generation in Go using arrays. Understand declaring arrays, returning sequences from functions, and iterating to calculate each Fibonacci value. Gain the skills to use arrays effectively for numerical sequences and prepare for learning slices next.

We'll cover the following...
Go (1.6.2)
package main
import "fmt"
var fib [10]int64 // global array containing Fibonacci values
func fibs() [10]int64{
fib[0] = 1 // base cases for 0
fib[1] = 1 // base case for 1
for i:= 2; i <10; i++ {
fib[i] = fib[i-1] + fib[i-2] // recursive case without recursion using array
}
return fib
}
func main() {
arr := fibs()
for i:=0; i < 10; i++ {
fmt.Printf("The %d-th Fibonacci number is: %d\n", i, arr[i])
}
}

In the code ...