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.

3.2 Serving Go

3.2.1 The Go web server

3.2.2 Serving through HTTPS

3.3 Handlers and handler functions

3.3.1 Handling requests

3.3.2 More handlers

3.3.3 Handler functions

3.3.4 Chaining handlers and handler functions

3.3.5 ServeMux and DefaultServeMux

3.3.6 Other multiplexers

3.4 Using HTTP/2

3.5 Summary

sitemap