How to have an optional query in GET request using Gorilla Mux?
Just a comment to the previous answer.
We can just add two routes there, I feel it is more readable, like below:
r.HandleFunc("/user", userByValueHandler).
Queries(
"username", "{username}",
"email", "{email}",
).
Methods("GET")
r.HandleFunc("/user", UserByValueHandler).Methods("GET")
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")
.....
}