【问题标题】:passing named parameters to index template while using httprouter golang package在使用 httprouter golang 包时将命名参数传递给索引模板
【发布时间】:2020-09-19 16:58:28
【问题描述】:

我刚刚学会了如何使用 httprouter go 包并阅读了很多关于它的文档,但是 当涉及到索引时,未能使用传递参数脚趾模板的 :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' 中的现有子级冲突

【问题讨论】:

标签: go templates server named-parameters httprouter


【解决方案1】:

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

【讨论】:

  • 非常感谢@Riley Worstell,我终于求助于通过 URL 查询参数接收电子邮件,它工作得很好。
猜你喜欢
  • 2016-06-03
  • 2019-12-31
  • 2011-04-18
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 1970-01-01
相关资源
最近更新 更多