Go Program to Print Fibonacci Series
A Go program to print the Fibonacci series uses a loop to generate numbers where each number is the sum of the two before it, like
for i := 0; i < n; i++ { fmt.Print(a, " "); a, b = b, a+b }.Examples
Input5
Output0 1 1 2 3
Input1
Output0
Input0
Output
How to Think About It
To print the Fibonacci series, start with the first two numbers 0 and 1. Then, for each next number, add the previous two numbers together. Repeat this until you reach the desired count.
Algorithm
1
Get the number of terms to print (n).2
Initialize two variables with 0 and 1 for the first two Fibonacci numbers.3
Use a loop to run n times.4
In each loop, print the current number.5
Update the two variables to the next two Fibonacci numbers by adding them.6
End the loop after printing n numbers.Code
go
package main import "fmt" func main() { n := 10 a, b := 0, 1 for i := 0; i < n; i++ { fmt.Print(a, " ") a, b = b, a+b } }
Output
0 1 1 2 3 5 8 13 21 34
Dry Run
Let's trace printing the first 5 Fibonacci numbers through the code
1
Initialize variables
a = 0, b = 1, n = 5
2
First iteration (i=0)
Print a=0; update a=1, b=1
3
Second iteration (i=1)
Print a=1; update a=1, b=2
4
Third iteration (i=2)
Print a=1; update a=2, b=3
5
Fourth iteration (i=3)
Print a=2; update a=3, b=5
6
Fifth iteration (i=4)
Print a=3; update a=5, b=8
| Iteration | a (printed) | b | Next a | Next b |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 2 | 3 |
| 3 | 2 | 3 | 3 | 5 |
| 4 | 3 | 5 | 5 | 8 |
Why This Works
Step 1: Start with first two numbers
The Fibonacci series begins with 0 and 1, so we set variables a = 0 and b = 1.
Step 2: Print current number
In each loop, we print the current value of a, which holds the current Fibonacci number.
Step 3: Update numbers for next iteration
We update a and b so that a becomes the old b, and b becomes the sum of old a and b, moving forward in the series.
Alternative Approaches
Recursive function
go
package main import "fmt" func fib(n int) int { if n <= 1 { return n } return fib(n-1) + fib(n-2) } func main() { n := 10 for i := 0; i < n; i++ { fmt.Print(fib(i), " ") } }
This method uses recursion but is slower and less efficient for large n due to repeated calculations.
Using slice to store series
go
package main import "fmt" func main() { n := 10 fibs := make([]int, n) fibs[0], fibs[1] = 0, 1 for i := 2; i < n; i++ { fibs[i] = fibs[i-1] + fibs[i-2] } for _, v := range fibs { fmt.Print(v, " ") } }
This stores all Fibonacci numbers in a slice, useful if you need to access them later but uses more memory.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs n times, so the time grows linearly with n, making it O(n).
Space Complexity
Only a few variables are used regardless of n, so space is constant O(1).
Which Approach is Fastest?
The iterative approach is fastest and most memory efficient compared to recursion or storing all values.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative loop | O(n) | O(1) | Fast and memory efficient printing |
| Recursive function | O(2^n) | O(n) | Simple code but slow and inefficient |
| Slice storage | O(n) | O(n) | When you need to reuse the series later |
Use multiple assignment like
a, b = b, a+b to update Fibonacci numbers cleanly in Go.Beginners often forget to update both numbers simultaneously, causing incorrect sequence values.