Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created January 28, 2022 06:59
Show Gist options
  • Save percybolmer/9103c7a5e0fbb1a17cdd6448ef4176be to your computer and use it in GitHub Desktop.
Save percybolmer/9103c7a5e0fbb1a17cdd6448ef4176be to your computer and use it in GitHub Desktop.
type ValuesRequest struct {
Values []int `json:"values"`
}
func CalculateHighest(w http.ResponseWriter, r *http.Request) {
// Declare a valuerequest
var vr ValuesRequest
// Decode and respond with error incase it fails
err := json.NewDecoder(r.Body).Decode(&vr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var high int
// Range all values
for _, value := range vr.Values {
// Check if value is higher than high
if value > high {
// If so, set high to value
high = value
}
}
// Return high
if high == 50 {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Something went wrong"))
}
fmt.Fprintf(w, "%d", high)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment