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
+13.6k Golang : Search folders for file recursively with wildcard support
+14.1k Golang : How to login and logout with JWT example
+5k Golang : Extract unicode string from another unicode string example
+21.4k Golang : simulate tail -f or read last line from log file example
+4.8k Get website traffic ranking with Similar Web or Alexa
+7.1k Golang : Emulate NumPy way of creating matrix example
+5k Golang : How to verify input is rune?
+9.1k Golang : Resolve domain name to IP4 and IP6 addresses.
+3.9k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+26.5k Get file path of temporary file in Go
+39.2k Golang : How to count duplicate items in slice/array?
+19.3k Golang : Convert date string to variants of time.Time type examples