Skip to content

Instantly share code, notes, and snippets.

@benhoyt
Created September 19, 2023 20:11
Show Gist options
  • Save benhoyt/0d37af14efa00d0a7a7d3ffac7bd16cf to your computer and use it in GitHub Desktop.
Save benhoyt/0d37af14efa00d0a7a7d3ffac7bd16cf to your computer and use it in GitHub Desktop.
Tiny Go web server to multiply two integers
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