Developing web clients
In this section, you will learn how to develop web clients in Go and how to time out a web connection that takes too long to finish.
Fetching a single URL
In this subsection, you will learn how to read a single web page with the help of the http.Get()
function, which is going to be demonstrated in the getURL.go
program. The utility will be presented in four parts; the first part of the program is the expected preamble:
package main import ( "fmt" "io" "net/http" "os" "path/filepath" )
Although there is nothing new here, you might find impressive the fact that you will use Go packages that are related to file input and output operations even though you are reading data from the internet. The explanation for this is pretty simple: Go has a uniform interface for reading and writing data regardless of the medium it is in.
The second part of getURL.go
has the following Go code:
func main() { if len(os.Args) != 2 { fmt.Printf("Usage: %s URL...