Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"sync"
)
var saveDir string = "/tmp"
func main() {
var wg sync.WaitGroup
for _, url := range os.Args[1:] {
wg.Add(1)
saveFile := strings.Split(url, "/")
fmt.Println("Download ", url)
go func() {
defer wg.Done()
err := download(url, saveDir+"/"+saveFile[len(saveFile)-1])
if err != nil {
log.Fatal(err)
return
}
}()
}
wg.Wait()
fmt.Println("finish.")
}
func download(url string, filename string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
_, err = io.Copy(file, res.Body)
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment