O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The net/http package contains the ServeMux struct, which implements the Handler interface to be used in a Server struct, but also contains the mechanism of how to define the handling of different paths. The ServeMux pointer contains the methods HandleFunc and Handle, which accept the path, and the HandlerFunc function handles the request for the given path, or another handler does the same. 

See the preceding example for how these could be used. The Handler interface and HandlerFunc require implementing the function with request and response arguments. This way you get access to these two structures. The request itself gives access to Headers, the HTTP method, and other request parameters.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required