
Web development is one of the many areas where you can use Go. Many companies and projects use Go on the backend of their web applications, primarily for its speed, ease of use, and ecosystem of packages.
The network/http contains most of the features you’ll need to build web apps in Go. You can use other packages from the feature-rich standard library. The coding package handles low-level data conversion and html package allows you to interact with web pages.
Beyond that, Go’s third-party package ecosystem provides additional features to make web development easier.
1. The gin framework
Gin is one of the most popular Go web development packages. Gin is a high-performance micro-framework for building web applications and microservices in Go.
Gin is fast and provides built-in rendering, middleware, and JSON validation. It offers easy error handling and extensibility. You can document your Gin applications with the OpenAPI3 specification and swagger.
Gin offers a Martini-like API, and the project claims to be forty times faster. For microservices, you can reuse Gin’s modular components to develop request processing pipelines.
You can install the Gin framework with this command:
go get github.com/gin-gonic/gin
Here’s how to set up a simple request endpoint with the Gin framework.
import (
"github.com/gin-gonic/gin"
"log"
"net/http"
)func main() {
router := gin.Default()
router.GET("/hello", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{"success": "Successfully hit the endpoint"})
})
log.Fatalln(http.ListenAndServe(":8080", nil))
}
You can create a router instance with the Default method of Gin package. The OBTAIN The method for GET requests takes the path (endpoint) and a handler function declaration. This example function returns a 200 HTTP status code to the client and a successful JSON response in the response body.
2. The fiber frame
Fiber is an in-memory safe ExpressJS-like framework built on blazing fast fasthttp package. It offers excellent performance and targets both new and experienced Javascript backend developers.
Fiber offers most of the features you’ll need in a backend framework. It handles routing, request grouping, validation, template creation, hooks, error handling and much more. Fiber is stretchy and you can make fiber faster using custom encoder and decoder.
Install the latest version (v2) of the Fiber framework with this command:
go get github.com/gofiber/fiber/v2
Here’s how you can set up a simple GET request endpoint with the Fiber framework.
import "github.com/gofiber/fiber/v2"func main() {
app := fiber.New()
app.Get("/hello", func(ctx *fiber.Ctx) error {
return ctx.SendString("Hello")
})
log.Fatal(app.Listen(":8080"))
}
The New returns a new instance of a Fiber application. The Obtain the method consists of setting up OBTAIN requests. In this case, the /hello the endpoint returns the string Hello.
You configure the application to listen on port localhost port 8080 with the Listen app method.
3. The Iris frame
Iris is a cross-platform, efficient, comprehensive and well-designed web framework. You can use it to build APIs and portable high-performance web applications in Go. Like Fiber, Iris is ExpressJS inspired by some of Iris’ design patterns.
You can quickly build serverless applications with Iris and deploy them to AWS, Netlify, and many other services. The Iris package contains a CLI application that you can use to live-reload Iris models and monitor your application.
The Iris package has features that make development extremely easy. Iris has a Sinatra-like API that supports logging, routing, sessions, and websockets. It also supports GRPC, file serving, authentication, authorization, and testing features.
Run this command in your workspace terminal to install the Iris framework on your Go modules.
go get github.com/kataras/iris/[email protected]
Here’s how to configure a OBTAIN request with Iris framework to run on localhost port 8080.
import "github.com/kataras/iris/v12"func main() {
app := iris.New()
app.Handle("GET", "/hello", func(ctx iris.Context) {
_, err := ctx.JSON(iris.Map{"message": "hello"})
if err != nil {
return
}
})
err := app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
if err != nil {
return
}
}
The application variable is the instance of a new Iris application. The OBTAIN handler returns a JSON message to request on the /hello itinerary.
4. The Beego Framework
Beego is an easy-to-use, smart, and high-performance Go framework for building web applications. Beego simplifies the creation of modular applications. It comes with a built-in ORM (object relational mapper) and router, as well as templating functionality.
Beego integrates Go-specific functionality with interfaces and framework integration. It has a great API structure, optimized for speed with session and deployment support and internalization.
Beego is widespread and many companies, from Opera to Huawei, Tencent and Weico, use Beego in production.
You can use this command to install the Beego Framework in your project.
go get -u github.com/beego/beego/v2
Here’s how to set up a simple OBTAIN request the API endpoint with the Beego framework.
import "github.com/beego/beego/v2/server/web"type ControllerInstance struct {
web.Controller
}
func (controller *ControllerInstance) Get() {
controller.Ctx.WriteString("hello world")
}
func main() {
web.Router("/hello", &ControllerInstance{})
web.Run()
}
The Controller instance struct is the entry point of the Beego application. The Obtain the handler function contains the logic of a OBTAIN ask the /hello period. It returns the string “hello world” as the response.
5. The Revel Frame
Revel shines with the flexibility it offers for creating web applications. You can use Revel’s type-safe routing, create controllers, and use Go models with Revel.
Revel provides easy routing, JSON encoding and decoding, and session management features. It also includes functions to handle caching, debugging, and testing of web applications. Revel has a CLI package to create CLIs for your applications. You can serve static files with Revel and build chat applications with its Websocket functionality.
Install the Revel framework in your project directory with this command:
go get github.com/revel/revel
Setting up a Revel app is easy. Here is a simple handler function for a OBTAIN query endpoint with the Revel framework.
import (
"github.com/revel/revel"
)type Model struct {
Message string `json:"message"`
Description string `json:"description"`
}
type App struct {
*revel.Controller
}
func (app App) Hello() revel.Result {
model := Model{
Message: "success",
Description: "Hello!, World",
}
app.Response.ContentType = "application/json"
return app.RenderJSON(model)
}
The Application struct is the entry point of the Revel application. Your handlers will implement the structure of the application. The content type of the response is JSON and the Hello The handler function returns an encoded JSON structure.
Beware of the difference between routers and frameworks
You will find many web packages in the Go ecosystem, most of which are routers or frameworks. Routers are intended to receive requests via the HTTP protocol.
You will need additional packages to work with routers. Like the ones in this tutorial, most frameworks include built-in routers as well as other features.