e.POST("/users", func(c echo.Context) error { name := c.FormValue("name") email := c.FormValue("email") return c.String(http.StatusOK, fmt.Sprintf("name: %s, email: %s", name, email)) })
e.GET("/users/:id", func(c echo.Context) error { id := c.Param("id") c.Set("userID", id) userID := c.Get("userID").(string) return c.String(http.StatusOK, fmt.Sprintf("user ID: %s", userID)) })
func customMiddleware(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { log.Println("custom middleware") return next(c) } } e.GET("/", func(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") }, customMiddleware())This code snippet shows how to add a custom middleware function to the request chain. The `customMiddleware` function logs a message and passes the request to the next handler. The Context package library is part of the Echo web framework for Go, which provides a wide range of features and functionalities for building high-performance web applications. With its intuitive and easy-to-use APIs, Echo makes it easy to write clean and maintainable code, while also providing a robust environment for building professional-grade web applications.