Web development is one of many fields in which you can use Go. Many companies and projects use Go on the backend of their web applications, mainly for its speed, ease of use, and package ecosystem.

The net/http package has most of the functionality you'll need to build web applications in Go. You can use other packages from the feature-rich standard library. The encoding package handles low-level data conversion and the html package lets you interact with webpages.

Beyond this, Go’s ecosystem of third-party packages provides extra features to aid web development.

1. The Gin Framework

Gin framework homepage

Gin is one of Go's most popular web development packages. Gin is a highly performant micro-framework for building web applications and microservices in Go.

Gin is fast and provides built-in rendering, middleware, and JSON validation functionality. It boasts easy error management and extendability. You can document your Gin applications with the OpenAPI3 spec and swagger.

Gin features 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-handling 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 the gin package. The GET method for GET requests takes in 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 Framework

Fiber framework homepage

Fiber is a memory-safe, ExpressJS-like framework built on the blazingly quick fasthttp package. It offers great performance and targets beginners and experienced Javascript backend developers.

Fiber features most of the functionality you'll need in a backend framework. It handles routing, request grouping, validation, templating, hooks, error handling, and much more. Fiber is extendable, and you can make Fiber faster using a 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 method returns a new instance of a Fiber app. The Get method is for setting up GET requests. In this case, the /hello endpoint returns the string Hello.

You set up the app to listen on port localhost port 8080 with the Listen method of the app.

3. The Iris Framework

Iris framework homepage

Iris is a cross-platform, efficient, full-fledged, well-designed web framework. You can use it to build high-performant, portable APIs and web apps in Go. Like Fiber, Iris is ExpressJS inspires some of the design patterns in Iris.

You can build serverless apps quickly with Iris and deploy them on AWS, Netlify, and many other services. The Iris package has a CLI app that you can use for live reloading Iris templates and monitoring your app.

The Iris package has features that make development extremely easy. Iris has a Sinatra-like API which caters for logging, routing, sessions, and Websockets. It also supports GRPC, file serving, authentication, authorization, and testing functionality.

Run this command in the terminal of your workspace to install the Iris framework to your Go modules.

        go get github.com/kataras/iris/v12@lastest

Here's how you can set up a GET request with the Iris framework to run on localhost port 8080.

        import "github.com/kataras/iris/v12"
 
func main() {
    app := iris.New() // new Iris instance
 
    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 app variable is the instance of a new Iris app. The GET handler returns a JSON message to request on the /hello route.

4. The Beego Framework

Beego framework homepage

Beego is an easy-to-use, intelligent, and highly performant Go framework for building web apps. Beego makes building modular applications straightforward. It comes with a built-in ORM (object-relational mapper) and router, alongside templating functionality.

Beego integrates Go-specific capabilities with interfaces and struct embedding. 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 you can set up a simple GET request API endpoint with the Beego framework.

        import "github.com/beego/beego/v2/server/web"
 
type ControllerInstance struct {
    web.Controller // entry point for the beego app
}
 
func (controller *ControllerInstance) Get() {
    controller.Ctx.WriteString("hello world")
}
 
func main() {
    web.Router("/hello", &ControllerInstance{}) // mounting with beego
    web.Run() // running beego app
}

The ControllerInstance struct is the entry point of the Beego app. The Get handler function contains the logic for a GET request to the /hello endpoint. It returns the “hello world” string as a response.

5. The Revel Framework

Revel framework homepage

Revel shines for the flexibility it provides for building web applications. You can use Revel's type-safe routing, build controllers, and use Go templates with Revel.

Revel provides functionality for routing, easy JSON encoding and decoding, and session-handling. It also includes functions to handle caching, debugging, and testing of web apps. Revel has a CLI package for building CLIs for your apps. You can serve static files with Revel and build chat apps with its Websocket functionality.

Install the Revel framework in your project directory with this command:

        go get github.com/revel/revel

It's easy to set up a Revel app. Here's a simple handler function for a GET request endpoint with the Revel framework.

        import (
    "github.com/revel/revel"
)
 
type Model struct { // simple model struct
    Message string `json:"message"`
    Description string `json:"description"`
}
 
type App struct {
    *revel.Controller // entry point for the requests
}
 
func (app App) Hello() revel.Result {
    model := Model{
        Message: "success",
        Description: "Hello!, World",
    }
 
    app.Response.ContentType = "application/json"
    return app.RenderJSON(model)
}

The App struct is the entry point of the Revel app. Your handlers will implement the app struct. The response content type is JSON, and the Hello handler function returns an encoded JSON struct.

Beware the Difference Between Routers and Frameworks

You'll find many web packages in the Go ecosystem, most of which are routers or frameworks. Routers are for receiving requests through the HTTP protocol.

You'll need additional packages to work with routers. Like those in this tutorial, most frameworks include built-in routers alongside other functionality.