在HTTP vs Mux上使用HandleFunc

I'm new to go and want to set up some routing, as well as cors. I've been seeing two styles of doing so, one that initializes a multiplexer with NewServeMux and then assigns handlers with HandleFunc, and one that uses HandleFunc directly on http. This is what I mean:

mux := http.NewServeMux()
mux.HandleFunc("/api", apiFunc)
mux.HandleFunc("/", indexFunc)

vs

http.HandleFunc("/api", apiFunc)
http.HandleFunc("/", indexFunc)
http.ListenAndServe("127.0.0.1:3001", nil)

Are there any differences with these approaches? If they accomplish similar things, is one more common/pragmatic?

http.HandleFunc et al apply your handlers to a package-global instance of the ServeMux held in the http package, which http.ListenAndServe then starts. You can also create your own instance as you did in the first example, which gives you some more control and makes it easier to unit test. In the end, the choice is yours; the convenience functions and package globals are probably fine for smaller projects with a limited maintenance period, but for larger or longer-lived projects I would generally recommend managing your own instances of both ServeMux and Server.