Last active
December 27, 2015 06:08
-
-
Save adyatlov/7279087 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" | |
"log" | |
"net/http" | |
"os" | |
) | |
const urlTemplate = "http://www.apifito.net/zivica-otzyvy.php?id=%v" | |
const fileNameTemplate = "page_%v.html" | |
type task struct { | |
Url string | |
FileName string | |
} | |
func downloader(taskChan <-chan task) { | |
for { | |
myTask := <-taskChan | |
resp, err := http.Get(myTask.Url) | |
if err != nil { | |
continue | |
} | |
defer resp.Body.Close() | |
file, err := os.Create(myTask.FileName) | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
n, err := io.Copy(file, resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
log.Printf("Copied %v bytes from %v to %v\n", | |
n, | |
myTask.Url, | |
myTask.FileName) | |
} | |
} | |
func main() { | |
const numberOfdownloaders = 10 | |
taskChan := make(chan task) | |
for i := 0; i < numberOfdownloaders; i++ { | |
go downloader(taskChan) | |
} | |
for i := 1; i <= 530; i++ { | |
myTask := task{fmt.Sprintf(urlTemplate, i), | |
fmt.Sprintf(fileNameTemplate, i)} | |
taskChan <- myTask | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment