douhui8454 2019-04-19 11:23
浏览 31

转到-使用Sequence从URL下载文件

i am trying to learn some new stuff with GoLang, and got a litlebit stuck, probaly the reason is just that i am not very good at using arrays.

So heres what i want to do:

  1. Make variable.
  2. Download with that variable.
  3. Add ++1 for that variable
  4. Download with added 1

and loop it lets say 10 times.

I am all good with points 1 and two, but little stuck with 3 & 4. :).

all the files come in .pdf, thats why i made that strconv there.

I probaly should make somekind of Loop in main, and call DownloadFile function with some array parameters in there?

package main

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

func main() {
    url_id := strconv.Itoa(23430815+2)
    filename := url_id+".pdf"
    fileUrl := "https://someurLid="+url_id

    if err := DownloadFile(filename, fileUrl); err != nil {
        panic(err)
    }
    fmt.Println(fileUrl)
}


func DownloadFile(filepath string, url string) error {

    resp, err := http.Get(url)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    out, err := os.Create(filepath)
    if err != nil {
        return err
    }
    defer out.Close()

    _, err = io.Copy(out, resp.Body)
    return err
}
  • 写回答

2条回答 默认 最新

  • dongqian8265 2019-04-19 12:11
    关注

    Cerise Limón gave the answer and thats thats how it worked out.

        arr := make([]uint8, 3) //How many times it loops
    url_id := 23430815 //Starting from id, filename
    for range arr {
        filename := strconv.Itoa(url_id)+".pdf"
        fileUrl := "https://someurl?id="+strconv.Itoa(url_id)
    
        if err := DownloadFile(filename, fileUrl); err != nil {
            panic(err)
        }
        fmt.Println(fileUrl)
        url_id++
    }
    

    Thank you for pointing out where i should start! :).

    评论

报告相同问题?

悬赏问题

  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败