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
+21.8k Golang : Join arrays or slices example
+5.7k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+8.1k Swift : Convert (cast) Character to Integer?
+10k Golang : Use regular expression to get all upper case or lower case characters example
+10.9k How to test Facebook App on localhost ?
+6.4k Golang : Calculate diameter, circumference, area, sphere surface and volume
+11.8k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+4.7k Which content-type(MIME type) to use for JSON data
+21.6k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+4.6k Adding Skype actions such as call and chat into web page examples
+5.2k Javascript : Shuffle or randomize array example
+6.8k Golang : Decode XML data from RSS feed