State of the golang Gorilla mux framework

State of the golang Gorilla mux framework

What is Gorilla mux

Gorilla mux is powerful HTTP router and URL matcher for building Go web servers

Current condition of Gorilla mux

As of 9th December 2022, the Golang gorilla mux framework was archived(in other words, it's no longer maintained). It created an imbalance in the Golang ecosystem since Gorilla Mux is one of the most preferred frameworks among Go developers. This action took developers by surprise and has made them consider other options out there.

Other Golang frameworks to consider

As it stands, the Gin framework is the most used of all the Golang frameworks with over 7,300 forks, 6,700 stars, and used by over 123,000.

Gin is an HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster.

Below is a sample code of how to use the Gin framework.

package main

import (
  "net/http"

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

func main() {
  r := gin.Default()
  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })
  r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

Express-inspired web framework written in Go. Fiber is a Go web framework built on top of Fasthttp, the fastest HTTP engine for Go. It's designed to ease things up for fast development with zero memory allocation and performance in mind.

Below is a sample code of how to use the Fiber framework.

package main

import (
    "log"

    "github.com/gofiber/fiber/v2"
)

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

    app.Get("/", func (c *fiber.Ctx) error {
        return c.SendString("Hello, World!")
    })

    log.Fatal(app.Listen(":3000"))
}

Chi is a lightweight, idiomatic and composable router for building Go HTTP services. It's especially good at helping you write large REST API services that are kept maintainable as your project grows and changes. chi is built on the new context package introduced in Go 1.7 to handle signaling, cancelation and request-scoped values across a handler chain.

Below is a sample code of how to use the Chi framework.

package main

import (
    "net/http"

    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
)

func main() {
    r := chi.NewRouter()
    r.Use(middleware.Logger)
    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("welcome"))
    })
    http.ListenAndServe(":3000", r)
}

HttpRouter is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go.

Below is a sample code of how to use the HttpRouter framework.

package main

import (
    "fmt"
    "net/http"
    "log"

    "github.com/julienschmidt/httprouter"
)

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

func main() {
    router := httprouter.New()
    router.GET("/", Index)
    router.GET("/hello/:name", Hello)

    log.Fatal(http.ListenAndServe(":8080", router))
}

Fasthttp was designed for some high performance edge cases. Unless your server/client needs to handle thousands of small to medium requests per second and needs a consistent low millisecond response time fasthttp might not be for you. For most cases net/http is much better as it's easier to use and can handle more cases. For most cases you won't even notice the performance difference.

Below is a sample code of how to use the FastHttp framework.

package main

import (
    "fmt"

    "github.com/qiangxue/fasthttp-routing"
    "github.com/valyala/fasthttp"
)

func main() {
    router := routing.New()

    router.Get("/", func(c *routing.Context) error {
        fmt.Fprintf(c, "Hello, world!")
        return nil
    })

    panic(fasthttp.ListenAndServe(":8080", router.HandleRequest))
}

Conclusion

There are lots of options to choose from out there. To get a comprehensive list of all the Golang frameworks, you can visit github.com/iqquee/golang-web-frameworks.