I'm trying to find a way to create special routes to that would allow post request to work with a params ( /url/:param ).
I've tried something like this :
http.HandleFunc("/myroute/:myparam",myfunction)
And i was hopping to be able to get the params as of the http-request in side "myfunction" with req.Form
but it keeps on failing on me and i get 404'ed.
I recognize it is something strange to ask as i could pass my params in the body of the function, but for ease of use/display i've been asked to be able to pregenerate a list of static "routes" on some preset so that it can be distributed as different urls + optional params instead of one url for everybody + params..
It's not possible with the default Mux
, but you can use alternative routers like httprouter, Gorilla Mux or chi.
Here is an example of httprouter
usage:
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
)
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!
", ps.ByName("name"))
}
func main() {
router := httprouter.New()
router.GET("/hello/:name", Hello)
http.ListenAndServe(":8080", router)
}