3 Handling requests
This chapter covers
- Using the Go net/http library
- Serving out HTTP using the Go net/http library
- Understanding handlers and handler functions
- Working with multiplexers
Chapter 2 showed the steps for creating a simple internet forum web application. The chapter mapped out the various parts of a Go web application, and you saw the big picture of how a Go web application is structured. But there’s little depth in each of those parts. In the next few chapters, we’ll delve into the details of each of these parts and explore in depth how they can be put together.
In this and the next chapter, we’ll focus on the brains of the web application: the handlers that receive and process requests from the client. In this chapter, you’ll learn how to create a web server with Go, and then we’ll move on to handling requests from the client.
3.1 The Go net/http library
Although using a mature and sophisticated web application framework to write web applications is usually easy and fast, the same frameworks often impose their own -conventions and patterns. Many assume that these conventions and patterns are best practices, but best practices have a way of growing into cargo cult programming when they aren’t understood properly. Programmers following these conventions without understanding why they’re used often follow them blindly and reuse them when it’s unnecessary or even harmful.