How to have an optional query in GET request using Gorilla Mux?

后端 未结 2 1474
野趣味
野趣味 2020-12-31 17:22

I would like to have some of my query parameters be optional. As for now, I have

r.HandleFunc(\"/user\", userByValueHandler).
    Queries(
        \"usernam         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 18:03

    So I found the solution to re-write my logic as:

    r.HandleFunc("/user", UserByValueHandler).Methods("GET")
    

    And in UserByValueHandler we can have something like:

    func UserByValueHandler(w http.ResponseWriter, r *http.Request) {
           v := r.URL.Query()
    
           username := v.Get("username")
           email := v.Get("email")
           .....
    }
    

提交回复
热议问题