-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (39 loc) · 1.03 KB
/
main.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
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"fmt"
"net/http"
"strconv"
)
//API interface that gets the value that will be passed to the Func fibonacci
func fibonacciHandler(w http.ResponseWriter, r *http.Request) {
// Get the value of the 'n' query parameter
nStr := r.URL.Query().Get("n")
n, err := strconv.Atoi(nStr)
if err != nil {
http.Error(w, "Invalid value for 'n'", http.StatusBadRequest)
return
}
// Calculate the Fibonacci sequence up to the nth number
result := fibonacci(n)
// Write the Fibonacci sequence as a response
fmt.Fprintf(w, "Fibonacci sequence up to %d: %v", n, result)
}
//Func fibonacci generates a fibonacci sequence based on "fibonacci(n)" passed from the handler
func fibonacci(n int) []int {
sequence := make([]int, n)
if n > 0 {
sequence[0] = 0
}
if n > 1 {
sequence[1] = 1
}
for i := 2; i < n; i++ {
sequence[i] = sequence[i-1] + sequence[i-2]
}
return sequence
}
func main() {
http.HandleFunc("/fibonacci", fibonacciHandler)
fmt.Println("Server is running on port 8080")
http.ListenAndServe(":8080", nil)
}