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
+8.7k Golang : Simple histogram example
+7.8k Golang : HTTP Server Example
+9.2k Golang : Changing a RGBA image number of channels with OpenCV
+10.9k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+7.4k Golang : get the current working directory of a running program
+11.4k Golang : GTK Input dialog box examples
+9.3k Random number generation with crypto/rand in Go
+7.9k Golang : Qt splash screen with delay example
+5.4k Linux/Unix/PHP : Restart PHP-FPM
+7.3k Golang : Rename part of filename
+6.4k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+11.3k Golang : Secure file deletion with wipe example