Echo_リクエストパラメータの受け取り方

リクエストパラメータの受け取り方のメモ

ルーティング

こんな感じのルーティングが設定されているときの、各ハンドラでのリクエストパラメータの受け取り方。

1
2
3
4
5
6
7
8
9
func main() {
e := echo.New()

e.GET("/item/:id", getItem)
e.GET("/items", getItems)
e.POST("/item", createItem)

e.Logger.Fatal(e.Start(":8080"))
}

URLの最後でパラメーターを渡す

1
2
3
4
func getItem(c echo.Context) error {
id := c.Param("id")
return c.String(http.StatusOK, "id = "+id)
}

GETでクエリーストリングでパラメーターを渡す

1
2
3
4
func getItems(c echo.Context) error {
name := c.QueryParam("name")
return c.String(http.StatusOK, "name = "+name)
}

POSTのパラメーターを渡す

1
2
3
4
func createItem(c echo.Context) error {
name := c.FormValue("name")
return c.String(http.StatusOK, "create name = "+name)
}