Skip to content

Instantly share code, notes, and snippets.

@vaguecoder
Last active May 21, 2021 19:09
Show Gist options
  • Save vaguecoder/03ecd15c42666cb7594790fa263e532f to your computer and use it in GitHub Desktop.
Save vaguecoder/03ecd15c42666cb7594790fa263e532f to your computer and use it in GitHub Desktop.
Basic fibonacci code snippet with works in goroutines which doesn't give whole series, but n'th number. More details in: https://github.com/VagueCoder/Random-Go-Snippets/
package fibonacci
import "sync"
func Fibonacci(n int) int {
var wg sync.WaitGroup
var mu sync.Mutex
var a, b int = 0, 1
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
mu.Lock()
defer mu.Unlock()
b += a
a = b - a
wg.Done()
}()
}
wg.Wait()
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment