路由就是对应我们访问请求的地址转发到后台处理, 如:http://127.0.0.1/hello
一、路由的使用
Echo.method(path string, h Handler)
// Handler func hello(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") } // Route e.GET("/hello", hello)
二、访问方法
常见GET、POST,还有PUT、DELETE。
还有任意方法可访问的Any
e.GET("/users", findUser) e.POST("/users", findUser) e.Any("/users", findUser)
package d // Handlers func createUser(c echo.Context) error { } func findUser(c echo.Context) error { } func updateUser(c echo.Context) error { } func deleteUser(c echo.Context) error { } // Routes e.POST("/users", d.createUser) e.GET("/users", findUser) e.PUT("/users", updateUser) e.DELETE("/users", deleteUser)
三、路由组
g := e.Group("/admin") g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) { if username == "joe" && password == "secret" { return true, nil } return false, nil })) g.POST("/users", createUser)
四、路由的参数
/users/:name e.GET("/users/:name", func(c echo.Context) error { name := c.Param("name") return c.String(http.StatusOK, name) })
五、路由与应用规划
routes/ a.router apps/ controller a.go b.go router.LoginRouter(e)