httprouter does not allow this and panics when start.
httprouter :panic: wildcard route ':name' conflicts with existing children in path '/hello/:name'.
In my understanding,
"/hello/:name" has two parts ,but /hello/b/c has three parts.
"/hello/:name" will match any url has two parts which prefix /hello
"/hello/b/c" only match /hello/b/c
package main
import (
"fmt"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Index2(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome222!\n")
}
func main() {
router := httprouter.New()
router.GET("/hello/b/c", Index2)
router.GET("/hello/:name", Index)
log.Fatal(http.ListenAndServe(":8080", router))
}
You could remove the line
router.GET("/hello/b/c", Index2)
and then have: