HTTP Client
package main /* * Download a URL. Mostly swiped from official example * https://golang.org/pkg/net/http/#example_Get */ import ( "fmt" "io/ioutil" "net/http" "os" ) func main() { // Get the document. url := os.Args[1] res, err := http.Get(url) // Check if we failed to get a response. if err != nil { fmt.Println("Network Error:", err) return } // Needs to be cleaned up after we're done. defer res.Body.Close() // Check if that response was a success code. if res.StatusCode / 100 != 2 { fmt.Println("Request Failed:", res.Status) return } // See if we've got text. mimetype := res.Header.Get("Content-Type") if mimetype[0:5] != "text/" { fmt.Println("Not text:", mimetype) return } fmt.Println("====== Document type:", mimetype, "=======") // Dump to the screen. May be a more efficient way to do this. bod, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println("Body read failed:", err) } fmt.Printf("%s\n", bod) // NOTE: The bod is an array of character codes. Use of %s in // Printf essentially converts to string before printing. }