The HTTP package
Due to its importance and ubiquity, HTTP is one of a handful of protocols directly implemented in Go. The net/http
package (https://golang.org/pkg/net/http/) provides code to implement both HTTP clients and HTTP servers. This section explores the fundamentals of creating HTTP clients and servers using the net/http
package. Later, we will return our attention back to building versions of our currency service using HTTP.
The http.Client type
The http.Client
struct represents an HTTP client and is used to create HTTP requests and retrieve responses from a server. The following illustrates how to retrieve the text content of Beowulf from Project Gutenberg's website located at http://gutenberg.org/cache/epub/16328/pg16328.txt, using the client
variable of the http.Client
type and prints its content to a standard output:
func main() { client := http.Client{} resp, err := client.Get( " http://gutenberg.org/cache/epub/16328/pg16328.txt") if...