2016-12-07 03:38

net / http:是否可以具有http.HandleFunc自定义参数?


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 :


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..

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duanbu1421 duanbu1421 5年前

    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 (
    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)
    点赞 评论 复制链接分享