In this Youtube video (at around 15:29) of a Golang talk by Blake Mizerany, he talks about how to build a router without using a third party package, covering in detail how to construct a route that has a variable component, such as an id. This is the handler that he uses, with the first line showing how to get the variable component of the route (i.e. the key)
func productHandler(w http.ResponseWriter, r *http.Request){
key := r.URL.Path[len("/products/":]
switch r.Method{
case "GET":
//do stuff
case "POST"
//do stuff
default:
http.Error(w, "method not allowed", 405)
}
}
It's not clear from his presentation though what his actual route looks like.
I'm trying to build a route that handles a put request with an id. When I click an element on my page, it sends a put request to this route
http://localhost:8080/products/1433255183951
I have a route like this
http.HandleFunc("/products/{id}", doSomethingWithProduct){
}
and of course have the func
func doSomethingWithProduct(res http.ResponseWriter, req *http.Request{
key := req.URL.Path[len("/products/"):]
log.Println(key, "is this logging?? nope")
}
Problem. Even though I have that route set up, and the handler, when I click the element I got a 404 not found, and there's no indication that my function was called (i.e. it's not logging)
Question: How do I create a custom route/func that handles a PUT request to
http://localhost:8080/products/1433255183951