The basic HTTP request

This example uses the http.Get() function from the net/http standard library package. It will read the entire response body to a variable named body and then print it to standard output:

package mainimport (   "fmt"   "io/ioutil"   "log"   "net/http")func main() {   // Make basic HTTP GET request   response, err := http.Get("http://www.example.com")   if err != nil {      log.Fatal("Error fetching URL. ", err)   }   // Read body from response   body, err := ioutil.ReadAll(response.Body)   response.Body.Close()   if err != nil {      log.Fatal("Error reading response. ", err)   }   fmt.Printf("%s\n", body)}

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.