SoFunction
Updated on 2025-04-07

Detailed explanation of Go routing registration method

Go routing registration method

mux := ()andIt is two different routing registration methods in Go language. Their differences are mainly reflected in the following aspects:

1. How to register routes

():

  • ()Create a newServeMuxExample (i.e. a new multiplexer).
  • pass()or()Register a route.
  • This method allows you to create multiple independent routers (ServeMux), each router can be used individually or in combination.
  • Example:
mux := ()
("/", rootHandler)
("/about", aboutHandler)

():

  • ()It is the default in the Go standard library directlyServeMux(Right now)。
  • pass()or()Register a route.
  • This way will register the route to the global default router, suitable for simple application scenarios.
  • Example:
("/", homeHandler)
("/about", aboutHandler)

2. Router independence

():

  • You can create multiple independentServeMuxInstances, each instance can be used individually or in combination.
  • For example, you can create different routers for different modules or functions and then combine them together.
  • Example:
mux1 := ()
("/api/v1", apiV1Handler)
mux2 := ()
("/api/v2", apiV2Handler)
// Combine multiple routersmainMux := ()
("/v1/", mux1)
("/v2/", mux2)

():

  • All routes are registered to the global default routermiddle.
  • This approach is not suitable for scenarios where modular or hierarchical routing is required.

3. Flexibility

():

  • More flexible and suitable for scenarios where a custom router is needed.
  • You can set different middleware or configurations for different routing groups.
  • Example:
mux := ()
("/", rootHandler)
("/admin", adminHandler)
// Use middlewareloggedMux := loggingMiddleware(mux)
(":8080", loggedMux)

():

  • Low flexibility and suitable for simple application scenarios.
  • All routes share the same global router and cannot set up different middleware or configurations for different routing groups.

4. How to start the server

():

  • When starting the server, you need to specify a custom one explicitly.ServeMux
  • Example:
mux := ()
("/", rootHandler)
(":8080", mux)

():

  • When starting the server, you do not need to specify the router explicitly, and use it by default.
  • Example:
("/", homeHandler)
(":8080", nil) // Use the default ServeMux

5. Applicable scenarios

():

  • Suitable for scenarios where modular, hierarchical routing or custom routers are required.
  • Suitable for large projects or projects that require flexible configuration.

():

  • Suitable for small projects or simple application scenarios.
  • Suitable for projects that are rapid prototyping or do not require complex routing configuration.

6. Code sample comparison

use()

package main
import (
	"fmt"
	"net/http"
)
func rootHandler(w , r *) {
	(w, "Root Handler")
}
func aboutHandler(w , r *) {
	(w, "About Handler")
}
func main() {
	mux := ()
	("/", rootHandler)
	("/about", aboutHandler)
	("Server is running on http://localhost:8080")
	(":8080", mux)
}

use()

package main
import (
	"fmt"
	"net/http"
)
func homeHandler(w , r *) {
	(w, "Home Handler")
}
func aboutHandler(w , r *) {
	(w, "About Handler")
}
func main() {
	("/", homeHandler)
	("/about", aboutHandler)
	("Server is running on http://localhost:8080")
	(":8080", nil)
}

Summarize

characteristic () ()
Router instance CustomizeServeMuxExample Use global default
flexibility High, supports modular and hierarchical routing Low, suitable for simple scenarios
Applicable scenarios Large projects or projects that require complex routing configuration Small project or rapid prototyping
Start the server method Need to specify explicitly customizationServeMux No explicit specification is required, the global router is used by default

Choose the right method according to project requirements: If flexibility and modularity are required, it is recommended to use();If the project is simple and does not require complex configuration, you can use it()

This is the end of this article about Go routing registration methods. For more relevant Go routing registration content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!