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.3k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+17.6k Golang : Convert PNG transparent background image to JPG or JPEG image
+6.9k Golang : Get final balance from bit coin address example
+16.5k Golang : How to force compile or remove object files first before rebuild?
+8.9k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+10.1k Golang : Encrypt and decrypt data with x509 crypto
+4k Python : Print unicode escape characters and string
+26.5k Golang : Get and Set User-Agent examples
+14.4k Golang : Capture stdout of a child process and act according to the result
+17.7k Nginx + FastCGI + Go Setup.
+7.5k Golang : Qt get screen resolution and display on center example
+4.3k Golang : Experimental emojis or emoticons icons programming language