Search code examples
gogorilla

Gorilla MUX routing with similar paths


I'm using Gorilla mux in my golang api for routing. I have two paths that are similar: /users/{id} and /users/settings. When I make a call to the /users/settings endpoint, it is getting routed to the endpoint /users/{id}. How do I fix this?

router := mux.NewRouter()
router.HandleFunc("/users/{id}", usersController.GetUserDetail).Methods(http.MethodGet)
router.HandleFunc("/users/settings", usersController.GetSettings).Methods(http.MethodGet)

Solution

  • Using the solution Gorilla MUX routing with similar paths. I switched the ordering and now I am registering the settings first.

    router.HandleFunc("/users/settings", usersController.GetSettings).Methods(http.MethodGet)
    
    router.HandleFunc("/users/{id}", usersController.GetUserDetail).Methods(http.MethodGet)