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
+6k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+31.8k Golang : Convert an image file to []byte
+6.3k Golang : Test input string for unicode example
+62.6k Golang : Convert HTTP Response body to string
+6.9k Golang : How to setup a disk space used monitoring service with Telegram bot
+4.5k Linux : sudo yum updates not working
+6.6k Golang : Skip or discard items of non-interest when iterating example
+17.6k How to enable MariaDB/MySQL logs ?
+18.1k Golang : Get command line arguments
+11.7k Golang : Verify Linux user password again before executing a program example
+26.2k Golang : Calculate future date with time.Add() function
+10.6k Golang : Flip coin example