concept http in category go

appears as: http, n http, http
Go in Practice

This is an excerpt from Manning's book Go in Practice.

HTTP, Representational State Transfer (REST), and web servers are incredibly common. To handle this common case, Go has the http package for providing both a client and a server (see the following listing). The client is simple enough to use that it meets the needs of the common everyday cases and extensible enough to use for the complex cases.

To handle static files, the http package in the standard library has a series of functions that deal with file serving. Before you look into a few techniques to serve files within an application, it’s important to know a little about the functionality built into the http package, shown in the next listing.

Listing 7.1. http package file serving: file_serving.go

The FileServer handler in the http package is a semi-smart file server. From a directory on the local filesystem, FileServer will serve files following proper permissions. It’s capable of looking at the If-Modified-Since HTTP header and responding with a 304 Not Modified response if the version of the file a user already has matches the one currently being served.

When you want to write your own handler to serve files, the ServeFile function in the http package is useful, as shown in the next listing.

tr := &http.Transport{
     TLSClientConfig:    &tls.Config{RootCAs: pool},
     DisableCompression: true,
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://example.com")