dongxue9997
2018-11-01 11:31
浏览 1.8k

在go中使用curl和命令

I'm using Go with command to execute curl which works as expected

curl := exec.Command("curl", "https://services.odata.org/V3/northwind/northwind.svc/")
    out, err := curl.Output()
    if err != nil {
        fmt.Println("erorr" , err)
        return
    }
    fmt.Println(out)

Now I want to use some placeholders like

curl -O http://quiet-waters-1228.herokuapp.com/assets/image.jpg

but now I need to get the url for command e.g. if I run in bash mytool url I got the url value

`curl -O $(mytool url)`

The problem is that we need to execute the command in the code and I'm not sure how to pass it

curl := exec.Command("curl", "curl -O $(url)")
        out, err := curl.Output()
        if err != nil {
            fmt.Println("erorr" , err)
            return
        }
        fmt.Println(out)

图片转代码服务由CSDN问答提供 功能建议

我正在使用Go with命令来执行 curl </ code>,它可以按预期工作</ p >

  curl:= exec.Command(“ curl”,“ https://services.odata.org/V3/northwind/northwind.svc/")
 out,错误:=  curl.Output()
如果错误!= nil {
 fmt.Println(“ erorr”,err)
 return 
} 
 fmt.Println(out)
 </ code> </ pre> \  n 
 

现在我要使用一些占位符,例如</ p>

  curl -O http://quiet-waters-1228.herokuapp.com/assets/image.jpg  
 </ code> </ pre> 
 
 

,但现在我需要获取命令的网址 e.g。 如果我在bash中运行 mytool url </ code>,我得到了url值</ p>

 `curl -O $(mytool url)`
 </ code> <  / pre> 
 
 

问题是我们需要在代码中执行命令,但我不确定如何传递它</ p>

  curl:=  exec.Command(“ curl”,“ curl -O $(url)”)
 out,err:= curl.Output()
如果err!= nil {
 fmt.Println(“ erorr”,err)  
 return 
} 
 fmt.Println(out)
 </ code> </ pre> 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题