英文:
Getting errors with net/url Values{}
问题
我正在处理我的第一个golang应用程序。我试图通过路由器获取一个URL值,并将其传递给一个URL,该URL将向API发出POST请求。所有这些都可以正常工作,但是当我尝试使用net/url
中的Value映射创建查询参数以便与POST请求一起发送时,我开始遇到错误。
以下是相关的代码:
package Utils
import (
"fmt"
"bytes"
"encoding/json"
"encoding/base64"
"log"
"net/http"
"net/url"
"io/ioutil"
)
...
func RetrieveAccessToken(client_id, client_secret, url, grant_type, code, redirect_uri string){
// 我们将与请求一起发送的数据
data := url.Values{}
data.Set("client_id", client_id)
data.Add("client_secret", client_secret)
data.Add("grant_type", grant_type)
data.Add("code", code)
data.Add("redirect_uri", redirect_uri)
...
}
以下是我遇到的错误:
Utils/Api.go:25:23: error: expected ';' or '}' or newline
data := url.Values{}
^
在此错误之后,对于前一个错误后的每一行,我都会得到类似以下的错误:
Utils/Api.go:26:5: error: expected declaration
data.Set("client_id", client_id)
^
直到最后,我得到了引用`net/url Values`调用的错误:
Utils/Api.go:25:16: error: reference to undefined field or method 'Values'
data := url.Values{}
^
如果有人能帮助我解决这个问题,我将非常感激。我已经看了一段时间的Go文档,看起来我一直在非常密切地遵循它们的示例。如果我误解了某个规则,或者我漏掉了什么,如果你能指点我正确的方向,我将非常感激!
再次感谢!
<details>
<summary>英文:</summary>
I am working on my first golang application. I am trying to take a url value through the router and push it along to a url which will make a POST request to a API. All of that works, but when I try to make query parameters to send with the POST request using the Value map from `net/url` I start to get errors.
Here is the relevant code:
package Utils
import (
"fmt"
"bytes"
"encoding/json"
"encoding/base64"
"log"
"net/http"
"net/url"
"io/ioutil"
)
...
func RetrieveAccessToken(client_id, client_secret, url, grant_type, code, redirect_uri string){
// The data we are going to send with our request
data := url.Values{}
data.Set("client_id", client_id)
data.Add("client_secret", client_secret)
data.Add("grant_type", grant_type)
data.Add("code", code)
data.Add("redirect_uri", redirect_uri)
...
}
And here are the errors I am getting
Utils/Api.go:25:23: error: expected ‘;’ or ‘}’ or newline
data := url.Values{}
^
After this error I get an error like this for every single line following the previous one:
Utils/Api.go:26:5: error: expected declaration
data.Set("client_id", client_id)
^
Until finally I get this error referencing that `net/url Values` call
Utils/Api.go:25:16: error: reference to undefined field or method ‘Values’
data := url.Values{}
^
If anyone could help me figure this out I would be very grateful. I have been looking at the Go docs for awhile, and it seems like I have been following their examples pretty closely. If there is a rule I am misunderstanding, or something I am missing I would be very thankful if you could point me in the right direction!
Thanks again!
</details>
# 答案1
**得分**: 18
你在函数中将“url”包名与`url`参数重名了。请将后者改名。
<details>
<summary>英文:</summary>
You've shadowed the "url" package name with the `url` argument in your function. Rename the latter.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论