Building Golang Restful API with Echo framework

Ankit Kumar Rajpoot
3 min readJul 26, 2022

I am assuming go already installed in your system, If it’s not installed you can install and set the path I am providing the link here.

Follow the below steps one by one.

Step 1:- Check the go version & make the folder

Go version should be greater than v1.13 because the lower version doesn’t support some middlewares, Make sure your project folder is outside your $GOPATH.

Check to go version

Make folder

Step 2:- Create server.go file in the root directory.

package mainimport (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
}

Step 3:- Start the server

go run server.go

You can browse the http://localhost:1323 and see the web page.

Step 4:- Routing

Write this code inside the server.go file.

// Define the routes
e.GET("/health", healthCheck)
e.GET("/users/:id", getUser) // Path Parameters
e.GET("/show", show) // Query Parameters
e.POST("/save", save) // application/x-www-form-urlencoded
e.POST("/saveUser", saveUser) // multipart/form-data
e.PUT("/users/:id", updateUser) // Path Parameters
e.DELETE("/users/:id", deleteUser) // Path Parameters

Now we will add one by one route codes with curl requests.

Route 1 -

// Health Check api
func healthCheck(c echo.Context) error {
return c.String(http.StatusOK, "Service is healthy!")
}

Curl-

curl --location --request GET 'http://localhost:1323/health'

Route 2 -

// Path Parameters
func getUser(c echo.Context) error {
// User ID from path `users/:id`
id := c.Param("id")
return c.String(http.StatusOK, id)
}

Curl

curl --location --request GET 'http://localhost:1323/getuser/1'

Route 3 -

// Query Parameters
func showUser(c echo.Context) error {
// Get team and member from the query string
team := c.QueryParam("team")
member := c.QueryParam("member")
return c.String(http.StatusOK, "team:"+team+", member:"+member)
}

Curl

curl --location --request GET 'http://localhost:1323/showuser?team=Voice&member=Ankit'

Route 4 -

// application/x-www-form-urlencoded
func save(c echo.Context) error {
// Get name and email
name := c.FormValue("name")
email := c.FormValue("email")
return c.String(http.StatusOK, "name:"+name+", email:"+email)
}

Curl

curl --location --request POST 'http://localhost:1323/saveUser' \
--form 'name="Ankit"' \
--form 'email="rajputankit22@gmail.com"'

Complete file-

package mainimport (
"github.com/labstack/echo/v4"
"net/http"
)
// Health Check api
func healthCheck(c echo.Context) error {
return c.String(http.StatusOK, "Service is healthy!")
}
// Path Parameters
func getUser(c echo.Context) error {
// User ID from path `users/:id`
id := c.Param("id")
return c.String(http.StatusOK, id)
}
// Query Parameters
func showUser(c echo.Context) error {
// Get team and member from the query string
team := c.QueryParam("team")
member := c.QueryParam("member")
return c.String(http.StatusOK, "team:"+team+", member:"+member)
}
// multipart/form-data
func saveUser(c echo.Context) error {
// Get name and email
name := c.FormValue("name")
email := c.FormValue("email")
return c.String(http.StatusOK, "name:"+name+", email:"+email)
}
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
// Define the routes
e.GET("/health", healthCheck)
e.GET("/getuser/:id", getUser) // Path Parameters
e.GET("/showuser", showUser) // Query Parameters
e.POST("/saveUser", saveUser) // multipart/form-data
// this line should be last
e.Logger.Fatal(e.Start(":1323"))
}

That’s it for this time! I hope you enjoyed this post. As always, I welcome questions, notes, comments and requests for posts on topics you’d like to read. See you next time! Happy Coding !!!!!

--

--

Ankit Kumar Rajpoot

I’m a MERN Developer. ( Redux | AWS | Python ) I enjoy taking on new things, building skills, and sharing what I’ve learned.