Okay, this is starting to look like something.
This commit is contained in:
57
functions/fibonacci.go
Normal file
57
functions/fibonacci.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package functions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HttpAbort(w http.ResponseWriter, code int, message string) {
|
||||||
|
w.WriteHeader(code)
|
||||||
|
fmt.Fprint(w, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MyFibonacci(count int) (fibonaccivalue int) {
|
||||||
|
fibonaccivalue = 1
|
||||||
|
var prev_fibonacci int = 0
|
||||||
|
|
||||||
|
for i := 1; i < count; i++ {
|
||||||
|
fibonaccivalue += prev_fibonacci
|
||||||
|
prev_fibonacci = fibonaccivalue - prev_fibonacci
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttFibonacci(w http.ResponseWriter, request *http.Request) {
|
||||||
|
switch request.Method {
|
||||||
|
case "POST":
|
||||||
|
error := request.ParseForm()
|
||||||
|
if error != nil {
|
||||||
|
fmt.Printf("Error parsing POST request: %s\n", error)
|
||||||
|
HttpAbort(w, http.StatusUnprocessableEntity,
|
||||||
|
"Unable to parse your POST request.\n")
|
||||||
|
} else {
|
||||||
|
s_seq := request.Form.Get("seq")
|
||||||
|
seq, error := strconv.Atoi(s_seq)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Printf("Error parsing integer value out of %s: %s\n",
|
||||||
|
s_seq,
|
||||||
|
error)
|
||||||
|
HttpAbort(w, http.StatusExpectationFailed,
|
||||||
|
fmt.Sprintf("[%s] is not a number.\n", s_seq))
|
||||||
|
} else if seq < 1 {
|
||||||
|
fmt.Printf("Invalid sequence number: %d\n", seq)
|
||||||
|
HttpAbort(w, http.StatusBadRequest,
|
||||||
|
"Please provide a positive integer as the sequence number.\n")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Received request for Fibonacci Sequence Number %d\n",
|
||||||
|
seq)
|
||||||
|
fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n",
|
||||||
|
seq, MyFibonacci(seq))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
HttpAbort(w, http.StatusMethodNotAllowed,
|
||||||
|
fmt.Sprintf("Operation %s not implemented for this URL", request.Method))
|
||||||
|
}
|
||||||
|
}
|
||||||
3
go.mod
Normal file
3
go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module darjeeling.systemec.nl/rhouben/fibonacci
|
||||||
|
|
||||||
|
go 1.24.4
|
||||||
45
main.go
Normal file
45
main.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"darjeeling.systemec.nl/rhouben/fibonacci/functions"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MyFibonacci(count int) (fibonaccivalue int) {
|
||||||
|
fibonaccivalue = 1
|
||||||
|
var prev_fibonacci int = 0
|
||||||
|
|
||||||
|
for i := 1; i < count; i++ {
|
||||||
|
fibonaccivalue += prev_fibonacci
|
||||||
|
prev_fibonacci = fibonaccivalue - prev_fibonacci
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var port_number int
|
||||||
|
var error error
|
||||||
|
var bind_socket string
|
||||||
|
fmt.Println("Hello, world. I'm teaching myself Go.")
|
||||||
|
|
||||||
|
if len(os.Args) <= 1 {
|
||||||
|
port_number = 8088
|
||||||
|
} else {
|
||||||
|
port_number, error = strconv.Atoi(os.Args[1])
|
||||||
|
|
||||||
|
if error != nil {
|
||||||
|
fmt.Printf("I had an error trying to parse %s into a number: %s\n",
|
||||||
|
os.Args[1],
|
||||||
|
error)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bind_socket = fmt.Sprintf(":%d", port_number)
|
||||||
|
http.HandleFunc("/fibonacci", functions.HttFibonacci)
|
||||||
|
fmt.Printf("Listening on port %d...", port_number)
|
||||||
|
http.ListenAndServe(bind_socket, nil)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user