From net/http GoDoc and Source.
ListenAndServe starts an HTTP server with a given address and handler. The handler is usually nil, which means to use DefaultServeMux. Handle and HandleFunc add handlers to DefaultServeMux
DefaultServeMux is the only predefined http.ServeMux
var DefaultServeMux = &defaultServeMux var defaultServeMux ServeMux
As you can see the http.Handle calls to DefaultServeMux internally.
func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }
The purpose of http.NewServeMux() is to have your own http.Servermux instance for instances, for example when you need two http.ListenAndServe functions that listen on different ports with different routes.
source share