Created
September 19, 2023 20:11
-
-
Save benhoyt/0d37af14efa00d0a7a7d3ffac7bd16cf to your computer and use it in GitHub Desktop.
Tiny Go web server to multiply two integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"strconv" | |
) | |
func main() { | |
http.HandleFunc("/multiply", func(w http.ResponseWriter, r *http.Request) { | |
query := r.URL.Query() | |
a, err := strconv.Atoi(query.Get("a")) | |
if err != nil { | |
http.Error(w, "'a' invalid", http.StatusBadRequest) | |
return | |
} | |
b, err := strconv.Atoi(query.Get("b")) | |
if err != nil { | |
http.Error(w, "'b' invalid", http.StatusBadRequest) | |
return | |
} | |
result := a * b | |
fmt.Fprintf(w, "%d\n", result) | |
}) | |
fmt.Println("listening on http://localhost:8081") | |
http.ListenAndServe(":8081", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment