Last active
September 5, 2021 17:51
-
-
Save agusrichard/2d0a198b4ffa21605df027f2d49fb4d8 to your computer and use it in GitHub Desktop.
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 ( | |
"encoding/json" | |
"net/http" | |
"strconv" | |
) | |
type Response struct { | |
Message string `json:"message"` | |
Result float64 `json:"result"` | |
} | |
// Main function to be tested | |
func doubleMe(x float64) float64 { | |
return x * 2 | |
} | |
// Main handler function that receives the request and returns the response | |
func handler(w http.ResponseWriter, r *http.Request) { | |
// Get the query string from the request | |
queryX := r.URL.Query().Get("x") | |
// Parse the input from user to float or returns error if it fails | |
x, err := strconv.ParseFloat(queryX, 64) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
// Send back the appropriate response | |
resp := Response{ | |
Message: "Hello!", | |
Result: doubleMe(x), | |
} | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(resp) | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment