Sling Academy
Home/Golang/Fibonacci Series with Loops in Go

Fibonacci Series with Loops in Go

Last updated: November 23, 2024

The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. It forms a fundamental part of various algorithms and provides an excellent exercise for understanding loops in Go.

What is a Fibonacci Series?

Let’s look at how the Fibonacci series begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The sequence starts with two fixed integers 0 and 1, and every subsequent number is the addition of the previous two numbers.

Basic Implementation in Go

To get started, here is a basic Fibonacci series generator using a simple for loop in Go:


package main

import "fmt"

func main() {
    // Initialize the first two numbers of the Fibonacci series
    a, b := 0, 1

    // Specify how many elements of the series you want to generate
    n := 10

    fmt.Printf("Fibonacci series for %d numbers:\n", n)

    for i := 0; i < n; i++ {
        fmt.Printf("%d ", a)
        next := a + b
        a = b
        b = next
    }
}

This simple Go program will print the first 10 numbers of the Fibonacci sequence. We make use of a basic for loop without using any auxiliary memory other than a few integer variables.

Intermediate Implementation with Function

To make our code reusable, we can encapsulate the logic of Fibonacci series generation within a function. This modular approach allows us to call a function whenever we need it.


package main

import "fmt"

// A function to generate Fibonacci series up to n numbers
func fibonacci(n int) []int {
    fibs := make([]int, n)
    a, b := 0, 1
    for i := 0; i < n; i++ {
        fibs[i] = a
        a, b = b, a+b
    }
    return fibs
}

func main() {
    fibs := fibonacci(10)
    fmt.Println("First 10 Fibonacci numbers:", fibs)
}

The Fibonacci function initializes a slice to store the Fibonacci numbers, iterates with a loop, fills the slice with Fibonacci numbers, and returns the slice. The main function then calls fibonacci to get the first 10 Fibonacci numbers and prints them.

Advanced Implementation using Goroutines

In Go, concurrency can be easily handled using goroutines, which can be a great tool for generating Fibonacci series, allowing for multiple sequences to be processed or fetched concurrently. Below is an advanced implementation:


package main

import (
    "fmt"
    "sync"
)

func fibonacci(n int, wg *sync.WaitGroup, ch chan<- int) {
    defer wg.Done()
    a, b := 0, 1
    for i := 0; i < n; i++ {
        ch <- a
        a, b = b, a+b
    }
    close(ch)
}

func main() {
    var n int = 10
    fmt.Printf("Printing %d Fibonacci numbers concurrently:\n", n)
    wg := new(sync.WaitGroup)
    ch := make(chan int)

    wg.Add(1)
    go fibonacci(n, wg, ch)

    for num := range ch {
        fmt.Println(num)
    }

    wg.Wait()
}

In this example, we created a goroutine fibonacci to send numbers to a channel ch and used a wait group wg to synchronize the end of all goroutines. This version pushes Fibonacci numbers to a channel, allowing them to be received and printed in the main function as soon as they are available.

This provides an excellent demonstration of Go’s built-in concurrency support.

Conclusion

Understanding how to generate a Fibonacci series using loops in Go helps improve your proficiency with looping constructs, function modularity, slices, and concurrent programming. From basic to advanced implementations, these examples provide insight into how Go can manage a simple algorithm in multiple ways, optimizing for reusability and concurrency.

Next Article: Debugging Scope Issues in Go: Common Mistakes and Fixes

Previous Article: Go - Iterating Collections: Using Range for Real-World Examples

Series: Variables & Control Flow

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant