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
+6.2k Golang : Fibonacci number generator examples
+9.2k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+9.5k Golang : Meaning of omitempty in struct's field tag
+7.6k PHP : How to parse ElasticSearch JSON ?
+10.9k Golang : How to detect a server/machine network interface capabilities?
+9.5k Golang : Resolve domain name to IP4 and IP6 addresses.
+5.3k Golang : Convert Chinese UTF8 characters to Pin Yin
+19.1k Golang : Count number of digits from given integer value
+4.7k Golang : Intercept, inject and replay HTTP traffics from web server
+9.2k Golang : Use regular expression to get all upper case or lower case characters example
+23.3k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+7.4k Golang : Reverse text lines or flip line order example