-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite.go
More file actions
53 lines (44 loc) · 858 Bytes
/
Copy pathwrite.go
File metadata and controls
53 lines (44 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package streamly
import (
"io"
"log"
"net/http"
"os"
)
func Write(file *os.File, prefix string, list []string) {
length := len(list)
data := make([]*io.ReadCloser, length)
fatched := make(chan bool, length+1)
fetch := func(index int, address string) {
defer func() { fatched <- true }()
conn, err := http.Get(address)
if err != nil {
log.Println(err)
data[index] = nil
return
}
data[index] = &conn.Body
}
for i, url := range list {
fetch(i, prefix+url)
}
done := 0
for _ = range fatched {
done++
if done == length {
close(fatched)
break
}
}
for _, chunk := range data {
io.Copy(file, *chunk)
(*chunk).Close()
}
}
func WriteAll(file *os.File, prefix string, list []string) {
length := len(list) + 4
for i := 4; i < length; i += 4 {
current := list[i-4 : i]
Write(file, prefix, current)
}
}