Fibonacci series program in Golang





Hey guys in this post, we will write a program to print first 10 fibonacci numbers using Golang.

package main

import "fmt"

func fibonacci() func() int {
	a, b := 0, 1
	return func() int {
		ret := a
		a, b = b, a+b
		return ret
	}
}

func main() {
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}

Output:

PS C:\workspace\go lang workspace> go run .\Fibonacci.go
0
1
1
2
3
5
8
13
21
34

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.



About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *