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
+20.8k Golang : Securing password with salt
+11.8k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+4.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+3.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+8.3k Golang : Changing a RGBA image number of channels with OpenCV
+18.2k Golang : Example for DSA(Digital Signature Algorithm) package functions
+7.2k Golang : Routes multiplexer routing example with regular expression control
+5k Golang : How to write backslash in string?
+15k Golang : How to extract links from web page ?
+40.6k Golang : Convert []byte to image
+8.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+4.3k Gogland : Datasource explorer