Golang Path Parameters
Created on April 4, 2026
go
Since go v1.22, you can use r.PathValue("id"). Previously, you needed to hand parse it from r.URL.Path.
since go v1.22
// DELETE /activities/{id}
func (app *application) bellevueActivityDelete(w http.ResponseWriter, r *http.Request) {
activityIDString := r.PathValue("id")
activityID, err := strconv.Atoi(activityIDString)
if err != nil {
app.serverError(w, r, fmt.Errorf("invalid activityID in path, could not parse: %v", err))
return
}
}
before go v1.22
// DELETE /activities/{id}
func (app *application) bellevueActivityDelete(w http.ResponseWriter, r *http.Request) {
// get ID from URL:
parts := strings.Split(r.URL.Path, "/")
// We expect: ["", "activities", "{ID}"]
if len(parts) != 3 {
log.Println("failed splitting request URL")
app.renderClientError(w, r, http.StatusBadRequest)
return
}
activityID := parts[2]