前回まで
というわけでGo版。こっちはちょっとね……。
オレオレmail-formをベース
main.goはここのをほぼ使いまわしている。
// gin init
// gin.SetMode(gin.ReleaseMode)
engine := gin.Default()
// load templates
engine.LoadHTMLGlob("templates/*")
// action
engine.GET("/:args", func(c *gin.Context) {
c.HTML(http.StatusOK, "test.html", gin.H{
"name": "index",
"args": c.Param("args"),
})
})
engine.GET("/category/:code", func(c *gin.Context) {
c.HTML(http.StatusOK, "test.html", gin.H{
"name": "category",
"code": c.Param("code"),
})
})
// 404
engine.NoRoute(func(c *gin.Context) {
c.HTML(http.StatusNotFound, "test.html", gin.H{
"name": "not found",
})
})
engine.Run(settings.Port)
これ、パニくって落ちます。結論を先に書くとASP.netと同じレベルで書くことはできないっぽい。
panic: 'category' in new path '/category/:code' conflicts with existing wildcard ':args' in existing prefix '/:args'
WildCardを同じに云々とかいうqiitaのエントリも見つけましたが、下記もダメです。
// gin init
gin.SetMode(gin.ReleaseMode)
engine := gin.Default()
// load templates
engine.LoadHTMLGlob("templates/*")
// action
engine.GET("/:param", func(c *gin.Context) {
c.HTML(http.StatusOK, "test.html", gin.H{
"name": "index",
"param": c.Param("param"),
})
})
engine.GET("/category/:param", func(c *gin.Context) {
c.HTML(http.StatusOK, "test.html", gin.H{
"name": "category",
"param": c.Param("param"),
})
})
// 404
engine.NoRoute(func(c *gin.Context) {
c.HTML(http.StatusNotFound, "test.html", gin.H{
"name": "not found",
})
})
engine.Run(settings.Port)
panic: 'category' in new path '/category/:param' conflicts with existing wildcard ':param' in existing prefix '/:param'
ググってたら、俺がやりたいことそのものをズバリと書いているblogを見つけた。Oh...
Goのginはルーティングに難がある:conflicts with existing wildcardのエラーが出たらさっさと別のライブラリを使おう - Irohabook
Goのお勉強用によいかなーと、PHP/c#/goという選択肢があってもかなりgo贔屓に考えていたんだけど……。もうちょっと考えよ。