Guide

Quick Start

Installation

  1. $ go get -u github.com/labstack/echo/...

Hello, World!

Create server.go

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/labstack/echo/v4"
  5. )
  6. func main() {
  7. e := echo.New()
  8. e.GET("/", func(c echo.Context) error {
  9. return c.String(http.StatusOK, "Hello, World!")
  10. })
  11. e.Logger.Fatal(e.Start(":1323"))
  12. }

Start server

  1. $ go run server.go

Browse to http://localhost:1323 and you should seeHello, World! on the page.

Routing

  1. e.POST("/users", saveUser)
  2. e.GET("/users/:id", getUser)
  3. e.PUT("/users/:id", updateUser)
  4. e.DELETE("/users/:id", deleteUser)

Path Parameters

  1. // e.GET("/users/:id", getUser)
  2. func getUser(c echo.Context) error {
  3. // User ID from path `users/:id`
  4. id := c.Param("id")
  5. return c.String(http.StatusOK, id)
  6. }

Browse to http://localhost:1323/users/Joe and you should see ‘Joe’ on the page.

Query Parameters

/show?team=x-men&member=wolverine

  1. //e.GET("/show", show)
  2. func show(c echo.Context) error {
  3. // Get team and member from the query string
  4. team := c.QueryParam("team")
  5. member := c.QueryParam("member")
  6. return c.String(http.StatusOK, "team:" + team + ", member:" + member)
  7. }

Browse to http://localhost:1323/show?team=x-men&member=wolverine and you should see ‘team:x-men, member:wolverine’ on the page.

Form application/x-www-form-urlencoded

POST /save

namevalue
nameJoe Smith
email[email protected]
  1. // e.POST("/save", save)
  2. func save(c echo.Context) error {
  3. // Get name and email
  4. name := c.FormValue("name")
  5. email := c.FormValue("email")
  6. return c.String(http.StatusOK, "name:" + name + ", email:" + email)
  7. }

Run the following command:

  1. $ curl -F "name=Joe Smith" -F "[email protected]" http://localhost:1323/save
  2. // => name:Joe Smith, email:[email protected]

Form multipart/form-data

POST /save

namevalue
nameJoe Smith
avataravatar
  1. func save(c echo.Context) error {
  2. // Get name
  3. name := c.FormValue("name")
  4. // Get avatar
  5. avatar, err := c.FormFile("avatar")
  6. if err != nil {
  7. return err
  8. }
  9. // Source
  10. src, err := avatar.Open()
  11. if err != nil {
  12. return err
  13. }
  14. defer src.Close()
  15. // Destination
  16. dst, err := os.Create(avatar.Filename)
  17. if err != nil {
  18. return err
  19. }
  20. defer dst.Close()
  21. // Copy
  22. if _, err = io.Copy(dst, src); err != nil {
  23. return err
  24. }
  25. return c.HTML(http.StatusOK, "<b>Thank you! " + name + "</b>")
  26. }

Run the following command.

  1. $ curl -F "name=Joe Smith" -F "[email protected]/path/to/your/avatar.png" http://localhost:1323/save
  2. // => <b>Thank you! Joe Smith</b>

For checking uploaded image, run the following command.

  1. cd <project directory>
  2. ls avatar.png
  3. // => avatar.png

Handling Request

  • Bind json, xml, form or query payload into Go struct based on Content-Typerequest header.
  • Render response as json or xml with status code.
  1. type User struct {
  2. Name string `json:"name" xml:"name" form:"name" query:"name"`
  3. Email string `json:"email" xml:"email" form:"email" query:"email"`
  4. }
  5. e.POST("/users", func(c echo.Context) error {
  6. u := new(User)
  7. if err := c.Bind(u); err != nil {
  8. return err
  9. }
  10. return c.JSON(http.StatusCreated, u)
  11. // or
  12. // return c.XML(http.StatusCreated, u)
  13. })

Static Content

Serve any file from static directory for path /static/*.

  1. e.Static("/static", "static")

Learn More

Template Rendering

Middleware

  1. // Root level middleware
  2. e.Use(middleware.Logger())
  3. e.Use(middleware.Recover())
  4. // Group level middleware
  5. g := e.Group("/admin")
  6. g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
  7. if username == "joe" && password == "secret" {
  8. return true, nil
  9. }
  10. return false, nil
  11. }))
  12. // Route level middleware
  13. track := func(next echo.HandlerFunc) echo.HandlerFunc {
  14. return func(c echo.Context) error {
  15. println("request to /users")
  16. return next(c)
  17. }
  18. }
  19. e.GET("/users", func(c echo.Context) error {
  20. return c.String(http.StatusOK, "/users")
  21. }, track)