diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bbee372 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module implementation.go + +go 1.22.3 diff --git a/main.go b/main.go new file mode 100644 index 0000000..0879582 --- /dev/null +++ b/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "net/http" + "net/url" + "strconv" + "strings" +) + +func calculateSum(number int) int { + sum := 0 + for i := 1; i <= number; i++ { + sum += i + } + return sum +} +func hello(w http.ResponseWriter, req *http.Request) { + myURL := "https://localhost:8090/?n=10" + parsedURL, err := url.Parse(myURL) + if err != nil { + fmt.Println("Can't parse URL", err) + return + } + parts := strings.Split(parsedURL.RawQuery, "=") + n, err := strconv.Atoi(parts[1]) + if err != nil { + fmt.Println("number is not found", err) + return + } + var res int = calculateSum(n) + fmt.Fprintf(w, "The sum from 1 to %d is: %d", n, res) +} + +func main() { + http.HandleFunc("/", hello) + http.ListenAndServe(":8090", nil) +}