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
+5.8k PHP : How to check if an array is empty ?
+13.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+7.8k Golang : Sort words with first uppercase letter
+28.5k Golang : Detect (OS) Operating System
+20.5k Golang : Saving private and public key to files
+9.3k Golang : Convert(cast) string to int64
+18.8k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+13.4k Golang : Query string with space symbol %20 in between
+19.8k Golang : Determine if directory is empty with os.File.Readdir() function
+12.4k Golang : Pass database connection to function called from another package and HTTP Handler
+24.3k Golang : Change file read or write permission example
+17.4k Golang : Parse date string and convert to dd-mm-yyyy format