Polling Websites and Reading Web Page
This lesson gives insight into how to check the status of a website with Go and how to read a page on the web.
We'll cover the following...
Introduction
Sending a website a very simple request and seeing how the website responds is known as polling a website.
Explanation
Consider the following example where a request includes only an HTTP header.
Press + to interact
package mainimport ("fmt""net/http")var urls = []string{"http://www.google.com/","http://golang.org/","http://blog.golang.org/",}func main() {// Executes an HTTP HEAD request for all URL's// and returns the HTTP status string or an error string.for _, url := range urls {resp, err := http.Head(url)if err != nil {fmt.Println("Error:", url, err)}fmt.Println(url, ": ", resp.Status)}}
In the program above, we import the package net/http
(see line 4). All URLs in an array of strings urls
(defined at line 7) are polled. At line 16, we start an iteration over urls
with a for-range loop. At line 17, a simple http.Head()
request is sent to each url to see how they react. The function’s signature is: func Head(url string) (r *Response, err error)
...