Skip to content

Instantly share code, notes, and snippets.

@Duncanian
Last active June 3, 2019 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Duncanian/c22acbfd2281705434c5c9d4d7e0afd8 to your computer and use it in GitHub Desktop.
Save Duncanian/c22acbfd2281705434c5c9d4d7e0afd8 to your computer and use it in GitHub Desktop.
Handling the endpoint for updating an event in go-rest-api
func updateEvent(w http.ResponseWriter, r *http.Request) {
eventID := mux.Vars(r)["id"]
var updatedEvent event
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update")
}
json.Unmarshal(reqBody, &updatedEvent)
for i, singleEvent := range events {
if singleEvent.ID == eventID {
singleEvent.Title = updatedEvent.Title
singleEvent.Description = updatedEvent.Description
events = append(events[:i], singleEvent)
json.NewEncoder(w).Encode(singleEvent)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment