Golang错误 – 未声明的名称:xxx编译器

huangapple go评论86阅读模式
英文:

Golang Error - Undeclared name: xxx compiler

问题

我正在编写一段 Golang 代码,用于执行一个在 bash 脚本中正确编写的 curl 命令。以下是我目前的代码(在 VSCode 中):

url := "https://<DOMAIN>/blah/blah/blah/" // <-- 在 bash 脚本中使用的有效 URL
cmd := exec.Command("curl", "-s --user <USERNAME> --request GET --url "+url+" --header 'Accept: application/json'")
out, err := cmd.Output()
if err != nil {
    fmt.Println("错误", err)
    return false
}
fmt.Println(out)

在我使用 out 和 err 的不同行上,出现了“未声明的名称:err(编译)”和“未声明的名称:out(编译)”的错误。我在这个帖子这里看到了这种格式的使用方式,所以我就是从那里得到的。我无法弄清楚在这种情况下正确使用 Stdin 或 Stdout。有人可以帮忙吗?

英文:

I am writing a snippet of Golang to perform a curl command that was correctly written in bash script. Here is what I have so far (in VSCode):

url=&quot;https://&lt;DOMAIN&gt;/blah/blah/blah/&quot; //&lt;--valid url, used in bash script and it worked
cmd := exec.Command(&quot;curl&quot;, &quot;-s --user &lt;USERNAME&gt; --request GET --url &quot;+url+&quot; --header &#39;Accept: application/json&#39;&quot;)
out, err = cmd.Output()
if err != nil {
	fmt.Println(&quot;erorr&quot;, err)
	return false
}
fmt.Println(out)

All of that ends up giving me the errors "undeclared name: err (compile)" and "undeclared name: out (compile)" on the different lines where I use out and err. I saw this format used on a post here so that's where I got this from. I can't figure out the right way to use Stdin or Stdout in this situation. Can anybody help?

答案1

得分: 3

你的编译错误是由这一行引起的,这一行试图给两个未声明的变量赋值:

out, err = cmd.Output()

这可能应该是一个短变量声明,即:

out, err := cmd.Output()

然而,一旦修复了这个问题,它仍然不会工作,因为你将所有的命令行参数作为一个单独的参数传递给了curl。应该是:

cmd := exec.Command("curl", "-s", "--user", "<USERNAME>", "--request", "GET", "--url", url, "--header", "Accept: application/json")

虽然从Go程序中调用curl相当不寻常;通常你会使用已经存在于net/http中的HTTP客户端,所以很可能所有这些代码都应该被替换为使用本地客户端。

英文:

Your compiler errors are due to this line, which tries to assign to two variables that (as the error says) are undeclared:

out, err = cmd.Output()

This should probably be a short variable declaration, i.e.:

out, err := cmd.Output()

However, once that's fixed, it still won't work, because you're passing all of your CLI params to curl as a single argument. It should be:

cmd := exec.Command(&quot;curl&quot;, &quot;-s&quot;, &quot;--user&quot;, &quot;&lt;USERNAME&gt;&quot;, &quot;--request&quot;, &quot;GET&quot;, &quot;--url&quot;, url, &quot;--header&quot;, &quot;Accept: application/json&quot;)

Though it's fairly unusual to invoke curl from a Go program; typically you'd use the HTTP client that already exists in net/http, so it's likely that all of this code should be replaced with use of the native client.

答案2

得分: 1

在你的代码中,你错过了冒号,应该使用:=而不是=

url = "https://<DOMAIN>/blah/blah/blah/" // <--有效的URL,在bash脚本中使用并且有效
cmd := exec.Command("curl", "-s --user <USERNAME> --request GET --url "+url+" --header 'Accept: application/json'")
out, err := cmd.Output()
if err != nil {
    fmt.Println("错误", err)
    return false
}
fmt.Println(out)
英文:

In your code you have missed the colon ,use := instead of =

url=&quot;https://&lt;DOMAIN&gt;/blah/blah/blah/&quot; //&lt;--valid url, used in bash script and it worked
cmd := exec.Command(&quot;curl&quot;, &quot;-s --user &lt;USERNAME&gt; --request GET --url &quot;+url+&quot; --header &#39;Accept: application/json&#39;&quot;)
out, err := cmd.Output()
if err != nil {
    fmt.Println(&quot;erorr&quot;, err)
    return false
}
fmt.Println(out)

huangapple
  • 本文由 发表于 2021年6月12日 02:23:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/67941872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定