Golang : http.Get example
Grabbing content in raw HTML format is useful for crawling or parsing purpose. This short tutorial demonstrates how easy it is to get the content of a website with http.Get()
function.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
// http.Get() can handle gzipped data response
// automagically
resp, err := http.Get("https://golang.org")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
htmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(os.Stdout, string(htmlData))
}
run this code and if everything goes well, you should see a bunch of HTML data being printed out.
NOTE : If you encounter crypto error regarding ssl connection, upgrade your Golang to latest version if problem persists, upgrade the certs on your machine
Reference :
See also : Golang : Download file example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+19.1k Golang : Check whether a network interface is up on your machine
+5.3k Golang : Pad file extension automagically
+8.8k Golang : Accept any number of function arguments with three dots(...)
+12.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+8.5k Golang : Another camera capture GUI application with GTK and OpenCV
+16k Golang : Generate universally unique identifier(UUID) example
+20.2k Golang : Check if os.Stdin input data is piped or from terminal
+19.7k Golang : Archive directory with tar and gzip
+8.3k Your page has meta tags in the body instead of the head
+10.8k Golang : Command line file upload program to server example
+11.1k Golang : Fix - does not implement sort.Interface (missing Len method)
+4.9k Python : Find out the variable type and determine the type with simple test