Skip to content

Instantly share code, notes, and snippets.

@chilts
Created August 22, 2011 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilts/1163510 to your computer and use it in GitHub Desktop.
Save chilts/1163510 to your computer and use it in GitHub Desktop.
Program to take a fully formed URL and print out the contents
package main
import (
"flag"
"os"
"http"
"fmt"
"io/ioutil"
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println("Specify a url.");
os.Exit(1);
}
res, err := http.Get(args[0])
if err != nil {
fmt.Println(err.String());
os.Exit(1);
}
defer res.Body.Close()
// read the entire file into a byte array
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err.String());
os.Exit(1);
}
// print them out
fmt.Println(string(bytes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment