-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.go
50 lines (41 loc) · 893 Bytes
/
net.go
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
package friendly
import (
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
)
// Downloads data from a url and returns it as a bytes array.
func GetFromUrlBytes(dlUrl string) ([]byte, error) {
_, err := url.Parse(dlUrl)
if err != nil {
return []byte{}, err
}
res, err := http.Get(dlUrl)
if err != nil {
return []byte{}, err
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
// Downloads data from a url and returns it as a string.
func GetFromUrlString(dlUrl string) (string, error) {
data, err := GetFromUrlBytes(dlUrl)
return string(data), err
}
// Downloads file from a url to given location.
func DownloadFile(dlUrl string, path string) error {
res, err := http.Get(dlUrl)
if err != nil {
return err
}
defer res.Body.Close()
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, res.Body)
return err
}