1 回答

TA貢獻(xiàn)1828條經(jīng)驗 獲得超3個贊
ioutil.ReadFile() 不支持 http。
如果您查看源代碼 ( https://golang.org/src/io/ioutil/ioutil.go?s=1503:1549#L42 ),請使用 os.Open 打開文件。
我想我可以做這個編碼。
package main
import (
"io"
"net/http"
"os"
)
func main() {
fileUrl := "http://www.pdf995.com/samples/pdf.pdf"
if err := DownloadFile("example.pdf", fileUrl); err != nil {
panic(err)
}
}
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
但是,go playgound 不是協(xié)議(go error dial tcp: Protocol not available)。
所以,你必須在電腦上做。
- 1 回答
- 0 關(guān)注
- 198 瀏覽
添加回答
舉報