Created
January 28, 2022 06:59
-
-
Save percybolmer/9103c7a5e0fbb1a17cdd6448ef4176be 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
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