Created
August 22, 2011 20:52
-
-
Save chilts/1163510 to your computer and use it in GitHub Desktop.
Program to take a fully formed URL and print out the contents
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 ( | |
"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