Skip to content

Instantly share code, notes, and snippets.

@blazs
Last active April 26, 2018 19:48
Show Gist options
  • Save blazs/30cae7cab12e9dd249dffe2b7fd8dbc8 to your computer and use it in GitHub Desktop.
Save blazs/30cae7cab12e9dd249dffe2b7fd8dbc8 to your computer and use it in GitHub Desktop.
A very primitive wget in Go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
url, filename := os.Args[1], os.Args[2]
resp, err := http.Get(url)
check(err, fmt.Sprintf("Error: %v\n", url, err), 1)
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
check(err, fmt.Sprintf("Error fetching %q: %v\n", url, err), 1);
err = ioutil.WriteFile(filename, body, 0644)
check(err, fmt.Sprintf("Error writing '%q': %v", filename, err), 1);
}
func check(err error, msg string, exitCode int) {
if err != nil {
fmt.Fprintf(os.Stderr, msg)
os.Exit(exitCode)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment