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
+4.4k Fix yum-complete-transaction error
+4.5k Unix/Linux : How to open tar.gz file ?
+6.8k Golang : Check if integer is power of four example
+32.9k Golang : How to split or chunking a file to smaller pieces?
+5.5k Fix sudo yum hang problem with no output or error messages
+3.8k HTTP common errors and their meaning explained
+4.9k Javascript : Generate random key with specific length
+32.7k Golang : Save image to PNG, JPEG or GIF format.
+7.2k Linux/Unix : fatal: the Postfix mail system is already running
+9k Golang : Roll the dice example
+5.6k Golang : Accessing dataframe-go element by row, column and name example
+7k Useful methods to access blocked websites