A common task in modern computing is downloading a file over the HTTP protocol. The following example shows how to quickly download a specific URL to a file.
Other common tools that accomplish this task are curl and wget:
package main
import (
"io"
"log"
"net/http"
"os"
)
func main() {
// Create output file
newFile, err := os.Create("devdungeon.html")
if err != nil {
log.Fatal(err)
}
defer newFile.Close()
// HTTP GET request devdungeon.com
url := "http://www.devdungeon.com/archive"
response, err := http.Get(url)
defer response.Body.Close()
// Write bytes from HTTP response to file.
// response.Body satisfies the reader interface.
// newFile satisfies the writer interface.
// That allows us to use io.Copy...