首页 > 解决方案 > 在使用 httprouter golang 包时将命名参数传递给索引模板

问题描述

我刚刚学习了如何使用httprouter go 包并阅读了许多有关它的文档,但是在涉及到索引页面模板时,未能使用传递参数 toe 模板的 :name 样式。

前任。

我的路由器代码:

    func getRouter() *httprouter.Router {
    // Load and parse templates (from binary or disk)
    templateBox = rice.MustFindBox("templates")
    templateBox.Walk("", newTemplate)

    // mux handler
    router := httprouter.New() 

    // Example route that encounters an error
    router.GET("/broken/handler", broken)
    
    // Index route
    router.GET("/:email", index)
    router.GET("/read/all", readingHandler)
    router.POST("/submit/newcon", Handler1) 
    router.POST("/read/newcon", Handler2)
     
    // Serve static assets via the "static" directory
    fs := rice.MustFindBox("static").HTTPBox()
    router.ServeFiles("/static/*filepath", fs)
    return router
}

然后我得到这个错误:

恐慌:通配符段 ':email' 与路径 '/:email' 中的现有子级冲突

标签: gotemplatesservernamed-parametershttprouter

解决方案


所以它应该与 /user/:email 一起工作,而不仅仅是 /:email。


推荐阅读