Created
October 4, 2018 08:01
-
-
Save ianfoo/ac72c0b2cb269b0f221ab332bee972d6 to your computer and use it in GitHub Desktop.
Fetch a URL and output to stdout
This file contains 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" | |
"net/http" | |
"os" | |
"path" | |
) | |
func main() { | |
for _, imgurl := range os.Args[1:] { | |
err := fetch(imgurl, os.Stdout) | |
if err != nil { | |
exit(err) | |
} | |
} | |
} | |
func fetch(url string, out io.Writer) error { | |
resp, err := http.Get(url) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
io.Copy(out, resp.Body) | |
return nil | |
} | |
func exit(err error) { | |
fmt.Fprintf(os.Stderr, "%s: %s\n", path.Base(os.Args[0]), err) | |
os.Exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment