0% found this document useful (0 votes)
26 views

Go Web Server

Uploaded by

Aiden
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Go Web Server

Uploaded by

Aiden
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023

Kakkar | Aug, 2023 | Medium

Golang Web Frameworks: A Comparative


Analysis of Gin, Echo, and Iris
Amber Kakkar · Follow
6 min read · 6 days ago

Listen Share

G olang, also known as Go, is a powerful and efficient programming language


known for its simplicity and performance. When it comes to building web
applications in Go, developers have a plethora of web frameworks to choose from.
Three of the most popular web frameworks in the Golang ecosystem are Gin, Echo,
and Iris. In this comprehensive guide, we will compare and contrast these
frameworks, discussing their features, strengths, and best use cases.

Frameworks for go

1. Gin:

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 1/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

Gin is a lightweight and fast web framework that aims to provide a minimalistic API
for building robust web applications. It is inspired by the Sinatra framework from
the Ruby world. Some key features of Gin include:

Fast Performance: Gin is designed to be blazingly fast, making it an excellent


choice for high-performance web applications.

Middleware Support: Gin has an easy-to-use middleware system that allows


developers to add common functionalities like logging, authentication, and
CORS handling.

Routing: The framework provides a flexible and easy-to-understand routing


system, allowing developers to define routes and their corresponding handlers
with ease.

Validation and Binding: Gin offers built-in validation and binding capabilities,
making it simple to parse and validate incoming data from HTTP requests.

Strengths:

Well-suited for building RESTful APIs and microservices due to its lightweight
nature and fast performance.

Beginner-friendly with a minimalistic and clean API, making it easy to learn and
use.

Best Use Cases:

Building APIs and microservices that require high performance and low
overhead.

Projects where a straightforward and minimalistic framework is preferred.

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 2/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

2. Echo:
Echo is another popular web framework for Golang, known for its simplicity, speed,
and robustness. It is inspired by the Express.js framework from the Node.js world.
Key features of Echo include:

Fast Router: Echo boasts an extremely fast router, which significantly improves
the routing performance.

Middleware Chaining: Echo provides an elegant middleware chaining system,


allowing developers to create complex middleware stacks.

Customizable: The framework is highly customizable, enabling developers to


configure it according to their specific needs.

WebSocket Support: Echo includes built-in support for handling WebSocket


connections, making it an excellent choice for real-time applications.

Strengths:

Ideal for building RESTful APIs, web applications, and WebSocket-based


applications.

Offers a good balance between performance and ease of use.


https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 3/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

Best Use Cases:

Real-time applications that require WebSocket support.

Web applications that need a well-documented and flexible framework.

3. Iris:
Iris is a feature-rich and fast web framework for Golang, known for its focus on
performance and extensibility. It is designed to be developer-friendly and has a wide
array of features. Key features of Iris include:

Superior Performance: Iris prides itself on being one of the fastest Golang web
frameworks available.

MVC Architecture: The framework follows the Model-View-Controller (MVC)


pattern, providing a structured approach to building applications.

Plugin System: Iris comes with a powerful plugin system, enabling developers to
extend the framework’s functionality easily.

Authentication and Security: Iris provides built-in support for authentication


and various security features.

Strengths:

Suitable for building large-scale web applications that require high performance
and extensive features.

Great choice for projects that need a modular and extensible framework.

Best Use Cases:

Large-scale web applications with complex requirements.

Projects where performance is a top priority.

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 4/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

To evaluate each framework’s capabilities, we’ll build a simple RESTful API for
managing “todo” items. This example will demonstrate how each framework
handles routing, request handling, and provides support for CRUD (Create, Read,
Update, Delete) operations. By the end of this article, readers will have a better
understanding of the strengths and best use cases for each web framework.

Objective: Build a RESTful API to manage “todo” items with basic CRUD operations
(Create, Read, Update, Delete).

1. Gin Example:

package main

import (
"github.com/gin-gonic/gin"
)

type Todo struct {


ID int `json:"id"`
Content string `json:"content"`
Done bool `json:"done"`
}

var todos []Todo

func main() {
r := gin.Default()

r.GET("/todos", getTodos)
r.POST("/todos", addTodo)
r.PUT("/todos/:id", updateTodo)
r.DELETE("/todos/:id", deleteTodo)

r.Run(":8080")
}

func getTodos(c *gin.Context) {


https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 5/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

c.JSON(200, todos)
}

func addTodo(c *gin.Context) {


var newTodo Todo
c.BindJSON(&newTodo)
todos = append(todos, newTodo)
c.JSON(201, newTodo)
}

func updateTodo(c *gin.Context) {


id := c.Param("id")
var updatedTodo Todo
c.BindJSON(&updatedTodo)
for i, todo := range todos {
if strconv.Itoa(todo.ID) == id {
todos[i] = updatedTodo
c.JSON(200, updatedTodo)
return
}
}
c.JSON(404, gin.H{"message": "Todo not found"})
}

func deleteTodo(c *gin.Context) {


id := c.Param("id")
for i, todo := range todos {
if strconv.Itoa(todo.ID) == id {
todos = append(todos[:i], todos[i+1:]...)
c.JSON(200, gin.H{"message": "Todo deleted"})
return
}
}
c.JSON(404, gin.H{"message": "Todo not found"})
}

2. Echo Example:

package main

import (
"net/http"

"github.com/labstack/echo/v4"
)

type Todo struct {


ID int `json:"id"`

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 6/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

Content string `json:"content"`


Done bool `json:"done"`
}

var todos []Todo

func main() {
e := echo.New()

e.GET("/todos", getTodos)
e.POST("/todos", addTodo)
e.PUT("/todos/:id", updateTodo)
e.DELETE("/todos/:id", deleteTodo)

e.Start(":8080")
}

func getTodos(c echo.Context) error {


return c.JSON(http.StatusOK, todos)
}

func addTodo(c echo.Context) error {


var newTodo Todo
c.Bind(&newTodo)
todos = append(todos, newTodo)
return c.JSON(http.StatusCreated, newTodo)
}

func updateTodo(c echo.Context) error {


id := c.Param("id")
var updatedTodo Todo
c.Bind(&updatedTodo)
for i, todo := range todos {
if strconv.Itoa(todo.ID) == id {
todos[i] = updatedTodo
return c.JSON(http.StatusOK, updatedTodo)
}
}
return c.JSON(http.StatusNotFound, map[string]string{"message": "Todo not f
}

func deleteTodo(c echo.Context) error {


id := c.Param("id")
for i, todo := range todos {
if strconv.Itoa(todo.ID) == id {
todos = append(todos[:i], todos[i+1:]...)
return c.JSON(http.StatusOK, map[string]string{"message": "Todo del
}
}
return c.JSON(http.StatusNotFound, map[string]string{"message": "Todo not f
}

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 7/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

3. Iris Example:

package main

import (
"github.com/kataras/iris/v12"
)

type Todo struct {


ID int `json:"id"`
Content string `json:"content"`
Done bool `json:"done"`
}

var todos []Todo

func main() {
app := iris.New()

app.Get("/todos", getTodos)
app.Post("/todos", addTodo)
app.Put("/todos/{id:int}", updateTodo)
app.Delete("/todos/{id:int}", deleteTodo)

app.Run(iris.Addr(":8080"))
}

func getTodos(ctx iris.Context) {


ctx.JSON(todos)
}

func addTodo(ctx iris.Context) {


var newTodo Todo
ctx.ReadJSON(&newTodo)
todos = append(todos, newTodo)
ctx.JSON(newTodo)
}

func updateTodo(ctx iris.Context) {


id, _ := ctx.Params().GetInt("id")
var updatedTodo Todo
ctx.ReadJSON(&updatedTodo)
for i, todo := range todos {
if todo.ID == id {
todos[i] = updatedTodo
ctx.JSON(updatedTodo)
https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 8/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

return
}
}
ctx.StatusCode(404)
ctx.JSON(map[string]string{"message": "Todo not found"})
}

func deleteTodo(ctx iris.Context) {


id, _ := ctx.Params().GetInt("id")
for i, todo := range todos {
if todo.ID == id {
todos = append(todos[:i], todos[i+1:]...)
ctx.JSON(map[string]string{"message": "Todo deleted"})
return
}
}
ctx.StatusCode(404)
ctx.JSON(map[string]string{"message": "Todo not found"})
}

Conclusion:
In conclusion,
Open in app choosing the right web framework for your Golang project
Signdepends
up Sign In
on your specific requirements and preferences. If you need a lightweight and fast
framework for building RESTful APIs or microservices, Gin is an excellent option.
For web applications that require WebSocket support and a balance between
performance and simplicity, Echo fits the bill. On the other hand, if your project
demands extensive features, performance, and a modular approach, Iris is a strong
contender.In this example, the differences between the frameworks are more
evident in the routing and request handling methods. Choose the framework that
best fits your project’s requirements, team expertise, and desired level of simplicity
versus extensibility. All three frameworks are excellent choices, and you can’t go
wrong with any of them.

Happy coding! 🚀

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 9/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

For more info checkout: https://github.com/evgesoch/gofwc

Golang Tutorial To Do List Golang Gin Hands On Tutorials

Follow

Written by Amber Kakkar


6 Followers

SDE-I @tiket || Research Intern at DRDO || DITU'22 || ❤ to Build!

More from Amber Kakkar

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 10/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

Amber Kakkar

Harnessing the Power of Concurrency: Exploring Goroutines and


Channels in Go
Concurrency is a fundamental feature of Go that enables developers to write highly efficient
and responsive applications. With its…

3 min read · Jul 11

Amber Kakkar in IEEE Student Branch DIT University

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 11/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

Blockchain
What is Blockchain?

4 min read · Jul 23, 2020

10

See all from Amber Kakkar

Recommended from Medium

Abhinav in Stackademic

Securing Go Applications: Best Practices and Libraries to Defend Against


Vulnerabilities
Security is paramount when developing applications, and Go provides various tools and
practices to help developers protect their…

· 3 min read · Aug 6

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 12/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

Prakash Rai in readytowork-org

Integrating Stripe Payment with Go(Gin), Gorm, MySQL, and React


Stripe is a technology company providing payment processing and related services to
businesses of all sizes with ease. It is one of the…

12 min read · Apr 23

12

Lists

General Coding Knowledge


20 stories · 201 saves

A Guide to Choosing, Planning, and Achieving Personal Goals


13 stories · 338 saves

Stories to Help You Grow as a Software Developer


19 stories · 269 saves

Visual Storytellers Playlist


35 stories · 43 saves

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 13/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

Abhinav

Building a Telegram Bot and Integrating it with a Golang: A Step-by-Step


Guide
Telegram is a popular messaging platform that allows users to create and interact with bots. In
this tutorial, we’ll walk you through the…

· 3 min read · Aug 6

Pascal Allen

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 14/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

JWT Authentication With Go


This publication is a walk-through of creating, validating, and refreshing JSON Web Tokens
using the HMAC signing method with Go.

3 min read · Jul 5

Gabi Sual

100 Go Mistakes and How to Avoid Them #3: Misusing init functions
What is init() function

4 min read · Aug 5

36

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 15/16
8/14/23, 10:15 AM Golang Web Frameworks: A Comparative Analysis of Gin, Echo, and Iris | by Amber Kakkar | Aug, 2023 | Medium

Denis Peganov

Testing With Ginkgo and Gomega


A simple guide of how to start your testing using Ginkgo and Gomega with real examples

8 min read · Mar 2

52 2

See more recommendations

https://medium.com/@amberkakkar01/golang-web-frameworks-a-comparative-analysis-of-gin-echo-and-iris-69fc793ca9d0 16/16

You might also like