public
Created

Fibonacci Go

  • Download Gist
fib.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
package main
 
import (
"http"
"fmt"
"log"
)
 
func fibonacci(n int) int {
result := 0
 
if (n < 2) {
result = 1
} else {
result = fibonacci(n - 2) + fibonacci(n - 1)
}
return result
}
 
func handler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(fmt.Sprintf("%d", fibonacci(40))))
}
func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServe(":12345", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err.String())
}
}

$ time curl http://localhost:12345
165580141
real 0m1.963s
user 0m0.003s
sys 0m0.004s

I'm getting 8s for nodejs

$ time curl http://localhost:1337/
165580141
real 0m7.952s
user 0m0.003s
sys 0m0.004s

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.