关于 go-curl 的 OPT_COOKIEJAR 选项的问题

Questions about go-curl's OPT_COOKIEJAR option

在go-curl中使用OPT_COOKIEJAR选项时,有没有办法检查工作是否完成?

如果你运行下面的源代码,有一个问题是在cookie下载完成之前执行了ReadFile。我想解决这个问题。

  CommonSetopt(easy)
  easy.Setopt(curl.OPT_VERBOSE, 0)
  easy.Setopt(curl.OPT_COOKIEJAR, cookieName)
  easy.Setopt(curl.OPT_URL, string("https://drive.google.com/uc?export=download&id="+gdriveID))

  log.Println("start download cookie: ", url)
  if err := easy.Perform(); err != nil {
      log.Println("cookie download fail: ", err)
      return
  }

  readBuf, err := ioutil.ReadFile(cookieName)
  if err != nil {
      log.Println("cookie read fail: ", err)
      return
  }

您需要在 easy.Perform 之后立即清理会话,而不是使用 defer 语句

进行清理
  CommonSetopt(easy)
  easy.Setopt(curl.OPT_VERBOSE, 0)
  easy.Setopt(curl.OPT_COOKIEJAR, cookieName)
  easy.Setopt(curl.OPT_URL, string("https://drive.google.com/uc?export=download&id="+gdriveID))

  log.Println("start download cookie: ", url)
  if err := easy.Perform(); err != nil {
      log.Println("cookie download fail: ", err)
      easy.Cleanup()
      return
  }
  // do cleanup
  easy.Cleanup()
  readBuf, err := ioutil.ReadFile(cookieName)
  if err != nil {
      log.Println("cookie read fail: ", err)
      return
  }