Last active
April 26, 2018 19:48
-
-
Save blazs/30cae7cab12e9dd249dffe2b7fd8dbc8 to your computer and use it in GitHub Desktop.
A very primitive wget in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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