我有這個評論,它下載了一個簡單的檔案:
var tarMode = "xf"
cmdEnsure = *exec.Command("cmd", "/C", fmt.Sprintf(`curl -L -o file.zip "https://drive.google.com/uc?export=download&id=theIDofthefile" && tar -%s file.zip`, tarMode))
err := cmdEnsure.Run()
這段代碼在旅途中會出錯,因為:
curl: (1) Protocol ""https" not supported or disabled in libcurl。
現在我明白這是由于我的雙引號引起的。但是,如果我洗掉 if,我會得到Id is not recognized as an internal or external command, operable program or batch file,這是有道理的,因為&簡單意味著在cmd.
那么我執行此類下載命令和提取的選項是什么?命令本身在cmd.
uj5u.com熱心網友回復:
我發現最好不要嘗試在一次執行中組合多個命令。下面的作業很好,您不必擔心轉義和可移植性,您還可以獲得更好的錯誤處理。
package main
import (
"fmt"
"os/exec"
)
func main() {
if b, err := exec.Command("curl", "-L", "-o", "file.zip", "https://drive.google.com/uc?export=download&id=theIDofthefile").CombinedOutput(); err != nil {
fmt.Printf("% v, %v", string(b), err)
return
}
if b, err := exec.Command("tar", "-x", "-f", "file.zip").CombinedOutput(); err != nil {
fmt.Printf("% v, %v", string(b), err)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/385992.html
