Setting up Echo
Getting started with Echo is straightforward. Let’s build a simple Hello World API using Echo to see how it works:
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{"hello": "world"})
})
e.Logger.Fatal(e.Start(":8080"))
}
This is all the code we need to build a Hello World API. Here, we have created a new instance of the Echo framework and registered a GET type handler, which returns a JSON response and starts the server. As you can see, it is straightforward and convenient. Methods such as c.JSON and the simplicity of the handler interface make the code easy to read and write.
Let’s talk more about this simplicity and the power of Echo handlers.