Skip to content

Instantly share code, notes, and snippets.

@gabrielrojasnyc
Last active Oct 22, 2017
Embed
What would you like to do?
file_go.go
package main
import (
//"io/ioutil"
"fmt"
"log"
"io"
"os"
"net/http"
"time"
"runtime"
)
func main() {
c := make(chan string)
start := time.Now()
log.Println("Starting")
links := map[string]string{
"Python-3.7.0a2.tgz": "http://ipv4.download.thinkbroadband.com/1GB.zip",
"Python-3.6.0.tgz": "http://ipv4.download.thinkbroadband.com/1GB.zip",
"Python-3.5.0.tgz": "http://ipv4.download.thinkbroadband.com/1GB.zip",
"Python-3.4.0.amd64.msi": "http://ipv4.download.thinkbroadband.com/1GB.zip",
"python-3.7.0a1.exe": "http://ipv4.download.thinkbroadband.com/1GB.zip",
"python-2.7.9rc1.amd64.msi": "http://ipv4.download.thinkbroadband.com/1GB.zip",
"python-2.7.9-macosx10.6.pkg": "http://ipv4.download.thinkbroadband.com/1GB.zip",
"python-3.6.2rc2-macosx10.6.pkg": "http://ipv4.download.thinkbroadband.com/1GB.zip",
"python-3.6.2rc1.exe": "http://ipv4.download.thinkbroadband.com/1GB.zip",
"python-3.6.2.exe": "http://ipv4.download.thinkbroadband.com/1GB.zip",
}
for n, link :=range(links){
go downloadFile(n, link, c)
}
for i := 0; i < len(links); i++ {
fmt.Println(<-c)
}
t := time.Now()
elapsed := t.Sub(start)
fmt.Println("total time to run the program", elapsed)
}
//Functions
func downloadFile(n string, link string, c chan string) {
resp, err := http.Get(link)
if err != nil {
log.Println("Could not download file", link)
c <- link
return
}
defer resp.Body.Close()
//Create file object
file, err := os.Create("/tmp/" + n)
if err != nil {
log.Fatal("Could not create file", n)
return
}
log.Println("Downloading File", n)
_, err = io.Copy(file, resp.Body)
//ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return
}
file.Close()
log.Println("File downloaded")
c <- link
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment