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
+6.5k Golang : Handling Yes No Quit query input
+7.3k Golang : How to check variable or object type during runtime?
+7.4k Golang : On lambda, anonymous, inline functions and function literals
+8.7k Golang : Identifying Golang HTTP client request
+8.6k Golang : Resumable upload to Google Drive(RESTful) example
+16.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+31.3k Golang : Smarter Error Handling with strings.Contains()
+7k Golang : Append and add item in slice
+17.2k Golang : Send email with attachment
+5.4k Golang : Break string into a slice of characters example
+11.4k Elastic Search : Return all records (higher than default 10)
+2k Golang : Fix go-cron set time not working issue