Go-wget

之前项目用到,考虑原生的wget有大小限制,所以用Goland写了个。

代码

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
/*
wget for goland
author:Hone
blog:http://hone.cool
*/

package main

import (
"io"
"net/http"
"os"
)

func DownloadFile(url string, filepath string) {

resp, err := http.Get(url)
check(err)
defer resp.Body.Close()

out, err := os.Create(filepath)
check(err)
defer out.Close()

// Write the body to file
_, err = io.Copy(out, resp.Body)
check(err)
}

func check(err error) {
if err != nil {
panic(err)
}
} //End check()

func main() {
DownloadFile(os.Args[1], os.Args[2])
}

用法:test.exe 远程下载地址 本地保存路径 (注:本地保存路径如有空格需用双引号括起来)
例:test.exe http://xxx.xxx.xxx/x.exe “C:\Documents and Settings\Administrator\Desktop\x.exe”

效果