Last active
August 29, 2015 14:13
-
-
Save ParthDesai/ca234ef854646ffacffa to your computer and use it in GitHub Desktop.
Download http data to file
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 ( | |
"bufio" | |
"net/http" | |
"os" | |
"fmt" | |
) | |
type Url struct { | |
url string | |
} | |
func (u *Url) download() (*http.Response, *bufio.Reader) { | |
resp, err := http.Get(u.url) | |
if err != nil { | |
panic(err) | |
} | |
return resp, bufio.NewReader(resp.Body) | |
} | |
func downloadUrl(url Url, out *os.File) { | |
resp, reader := url.download() | |
_,err := reader.WriteTo(out) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
} | |
func downloadUrlsToFile(url []string, file string) { | |
fo, err := os.Create(file) | |
if err != nil { | |
panic(err) | |
} | |
for _, value := range url { | |
downloadUrl(Url{value}, fo) | |
} | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
fmt.Println("Syntax: <program> <file-name>") | |
return | |
} | |
downloadUrlsToFile([]string{"http://www.google.com", "http://www.yahoo.com", "http://en.wikipedia.org"}, os.Args[1]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment