Gorilla Mux for Go: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Gorilla Mux and why is it needed?



Gorilla Mux (often called simply "mux" from "multiplexer") is a powerful and popular HTTP router for the Go programming language. It is part of the Gorilla Web Toolkit ecosystem, which provides many useful components for web development.



Why do you need a separate router if Go's standard library already has net/http? The standard http.ServeMux is great for simple projects, but has several limitations:

  • No support for dynamic path segments (e.g., /users/{id}).
  • No support for HTTP methods (GET, POST, PUT, DELETE) — you need to check manually.
  • No support for regular expressions in routes.
  • No built-in middleware.


Gorilla Mux solves all these problems. It allows you to:

  • Define complex routes with variable parts.
  • Restrict routes by HTTP methods, hosts, headers, and query parameters.
  • Use subrouters for grouping routes.
  • Easily integrate with middleware.
  • Build RESTful APIs with clean and understandable URLs.


The Gorilla Mux library is the de facto standard for building HTTP servers in Go. It is used in thousands of projects, from small microservices to large commercial applications.



Installation



Installing Gorilla Mux in a Go project is done via the go get package manager. Open a terminal in the root of your project and run the command:



go get -u github.com/gorilla/mux


This command will download the library and add it to your go.mod file. After that, you can import it in your code:



import "github.com/gorilla/mux"


If you are using Go Modules (the standard for Go 1.16+), the library will be automatically added to your go.mod and go.sum files. You're ready to go!



Quick Start: A Simple HTTP Server with Gorilla Mux



Let's create a minimal web server that handles GET requests to the root path and returns a greeting.



package main

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

"github.com/gorilla/mux")

func main() { // Create a new router r := mux.NewRouter()

// Define a route: GET / r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, world! This is Gorilla Mux!") })

// Start the server on port 8080 log.Println("Server started on http://localhost:8080") log.Fatal(http.ListenAndServe(":8080", r))}


Run this code (go run main.go) and open http://localhost:8080 in your browser. You will see the message "Hello, world! This is Gorilla Mux!".



Note: We pass our router r to http.ListenAndServe. This is possible because mux.Router implements the http.Handler interface.



Main Methods and Functions of Gorilla Mux



Let's look at the key methods of the library. Each method will be accompanied by a description, parameters, and a working example.



1. mux.NewRouter()

What it does: Creates a new router instance. This is the starting point for all routes.

Parameters: None.

Returns: A pointer to mux.Router.



r := mux.NewRouter()// Now r is ready to define routes


2. r.HandleFunc(path, handler)

What it does: Registers a handler function for a given path. This is the main way to define routes.

Par

Recommendations