Moving go code to subdir.

This commit is contained in:
2025-08-08 15:17:12 +02:00
parent 853bee22db
commit e8d042b65e
10 changed files with 17 additions and 24 deletions

View File

@@ -0,0 +1,21 @@
package functions
import(
"errors"
"fmt"
)
func MyFibonacci(count int) (fibonaccivalue int, error error) {
fibonaccivalue = 1
error = nil
var prev_fibonacci int = 0
if count < 1 {
return -1, errors.New(fmt.Sprintf("%d is not a positive integer.",count))
}
for i := 1; i < count; i++ {
fibonaccivalue += prev_fibonacci
prev_fibonacci = fibonaccivalue - prev_fibonacci
}
return
}

View File

@@ -0,0 +1,15 @@
package functions
import (
"flag"
)
func FlagSet(name string) (found bool) {
found = false
flag.Visit(func (f *flag.Flag) {
if f.Name == name {
found = true
}
})
return
}

View File

@@ -0,0 +1,52 @@
package functions
import (
"testing"
"fmt"
)
// Test fibonacci function
func TestFibonacci(t *testing.T) {
var sequence = [4]int{1, 3, 5, 9}
var values = [4]int{1, 2, 5, 34}
for i := 0; i < len(sequence); i++ {
var seq, want = sequence[i], values[i]
outcome, error := MyFibonacci(seq)
if error != nil {
t.Errorf("Error trying to Fibonacci %d: %s", seq, error)
}
if outcome != want {
t.Errorf("Invalid outcome: Fibonacci sequence %d should be %d, got %d",
seq, want, outcome)
}
}
}
func TestFibBadNumber(t *testing.T) {
outcome,error := MyFibonacci(-4)
if error == nil {
t.Errorf("This should be an error, not a %d",outcome)
}
}
func TestSums(t *testing.T) {
var tests = []struct{
a, b int
want int
} {
{1, 2, 3},
{5, 3, 8},
{9, 4, 13},
}
for _, tt := range tests {
testname := fmt.Sprintf("%d+%d",tt.a, tt.b)
t.Run(testname, func(t *testing.T) {
result := sum(tt.a, tt.b)
if result != tt.want {
t.Errorf("Expected %d, got %d", tt.want,result)
}
})
}
}

79
code/functions/http.go Normal file
View File

@@ -0,0 +1,79 @@
package functions
// HTTP helper functions doing the input checking and calling the actual math functions
import (
"fmt"
"net/http"
"strconv"
)
func HttpAbort(w http.ResponseWriter, code int, message string) {
w.WriteHeader(code)
fmt.Fprint(w, message)
}
func HttpGetPostInt(request *http.Request, key string) (value int, error error) {
switch request.Method {
case "POST":
error = request.ParseForm()
if error != nil {
error = fmt.Errorf("unable to parse post input: %s",
error.Error())
return
}
value, error = strconv.Atoi(request.Form.Get(key))
if error != nil {
fmt.Printf("Error parsing parameter '%s'\n", key)
error = fmt.Errorf("unable to parse parameter '%s' as an integer: %s",
key, error.Error())
}
return
default:
error = fmt.Errorf("this endpoint does not support %s operations", request.Method)
return
}
}
func HttFibonacci(w http.ResponseWriter, request *http.Request) {
seq, error := HttpGetPostInt(request, "seq")
if error != nil {
HttpAbort(w, http.StatusBadRequest,
fmt.Sprintf("Could not get parameter: %s", error.Error()))
} else if seq < 1 {
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)
value, error := MyFibonacci(seq)
if error != nil {
fmt.Printf("Error trying to Fibonacci %d: %s", seq, error)
fmt.Fprintf(w, "Error trying to Fibonacci %d: %s", seq, error)
} else {
fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n",
seq, value)
}
}
}
func HttSum(w http.ResponseWriter, request *http.Request) {
var errorstr string
first, error := HttpGetPostInt(request, "first")
if error != nil {
errorstr = error.Error()
}
second, error := HttpGetPostInt(request, "second")
if error != nil {
errorstr = error.Error()
}
if errorstr != "" {
HttpAbort(w, http.StatusBadRequest,
fmt.Sprintf("Invalid input: %s\n", errorstr))
return
}
fmt.Printf("Received a request to add %d and %d.\n", first, second)
fmt.Fprintf(w, "The sum of %d and %d is %d.\n", first, second, sum(first, second))
}

6
code/functions/sum.go Normal file
View File

@@ -0,0 +1,6 @@
package functions
func sum(first int, second int) (result int) {
result = first + second
return
}