Last active
October 22, 2017 22:40
-
-
Save gabrielrojasnyc/354d5fcb0ae5aa48a66fd3f448670cf9 to your computer and use it in GitHub Desktop.
file_go.go
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 ( | |
//"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