Created
December 10, 2014 11:12
-
-
Save erukiti/af6aeb952b83bb70bee5 to your computer and use it in GitHub Desktop.
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/ioutil" | |
"net/http" | |
"os" | |
"runtime" | |
"strings" | |
"sync" | |
) | |
func downloader(url string, wg *sync.WaitGroup) { | |
defer wg.Done() | |
tmp := strings.Split(url, "/") | |
fileName := tmp[len(tmp)-1] | |
resp, err := http.Get(url) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
ioutil.WriteFile(fileName, body, 0644) | |
} | |
func main() { | |
wg := new(sync.WaitGroup) | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
// url := "http://yukkurisinai.net/gopher.png" | |
for _, url := range os.Args[1:] { | |
wg.Add(1) | |
go downloader(url, wg) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment