使用net/url Values{}时出现错误。

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

Getting errors with net/url Values{}

问题

我正在处理我的第一个golang应用程序。我试图通过路由器获取一个URL值,并将其传递给一个URL,该URL将向API发出POST请求。所有这些都可以正常工作,但是当我尝试使用net/url中的Value映射创建查询参数以便与POST请求一起发送时,我开始遇到错误。

以下是相关的代码:

  1. package Utils
  2. import (
  3. "fmt"
  4. "bytes"
  5. "encoding/json"
  6. "encoding/base64"
  7. "log"
  8. "net/http"
  9. "net/url"
  10. "io/ioutil"
  11. )
  12. ...
  13. func RetrieveAccessToken(client_id, client_secret, url, grant_type, code, redirect_uri string){
  14. // 我们将与请求一起发送的数据
  15. data := url.Values{}
  16. data.Set("client_id", client_id)
  17. data.Add("client_secret", client_secret)
  18. data.Add("grant_type", grant_type)
  19. data.Add("code", code)
  20. data.Add("redirect_uri", redirect_uri)
  21. ...
  22. }

以下是我遇到的错误:

  1. Utils/Api.go:25:23: error: expected ';' or '}' or newline
  2. data := url.Values{}
  3. ^
  4. 在此错误之后,对于前一个错误后的每一行,我都会得到类似以下的错误:
  5. Utils/Api.go:26:5: error: expected declaration
  6. data.Set("client_id", client_id)
  7. ^
  8. 直到最后,我得到了引用`net/url Values`调用的错误:
  9. Utils/Api.go:25:16: error: reference to undefined field or method 'Values'
  10. data := url.Values{}
  11. ^
  12. 如果有人能帮助我解决这个问题,我将非常感激。我已经看了一段时间的Go文档,看起来我一直在非常密切地遵循它们的示例。如果我误解了某个规则,或者我漏掉了什么,如果你能指点我正确的方向,我将非常感激!
  13. 再次感谢!
  14. <details>
  15. <summary>英文:</summary>
  16. 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.
  17. Here is the relevant code:
  18. package Utils
  19. import (
  20. &quot;fmt&quot;
  21. &quot;bytes&quot;
  22. &quot;encoding/json&quot;
  23. &quot;encoding/base64&quot;
  24. &quot;log&quot;
  25. &quot;net/http&quot;
  26. &quot;net/url&quot;
  27. &quot;io/ioutil&quot;
  28. )
  29. ...
  30. func RetrieveAccessToken(client_id, client_secret, url, grant_type, code, redirect_uri string){
  31. // The data we are going to send with our request
  32. data := url.Values{}
  33. data.Set(&quot;client_id&quot;, client_id)
  34. data.Add(&quot;client_secret&quot;, client_secret)
  35. data.Add(&quot;grant_type&quot;, grant_type)
  36. data.Add(&quot;code&quot;, code)
  37. data.Add(&quot;redirect_uri&quot;, redirect_uri)
  38. ...
  39. }
  40. And here are the errors I am getting
  41. Utils/Api.go:25:23: error: expected ‘;’ or ‘}’ or newline
  42. data := url.Values{}
  43. ^
  44. After this error I get an error like this for every single line following the previous one:
  45. Utils/Api.go:26:5: error: expected declaration
  46. data.Set(&quot;client_id&quot;, client_id)
  47. ^
  48. Until finally I get this error referencing that `net/url Values` call
  49. Utils/Api.go:25:16: error: reference to undefined field or method Values
  50. data := url.Values{}
  51. ^
  52. 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!
  53. Thanks again!
  54. </details>
  55. # 答案1
  56. **得分**: 18
  57. 你在函数中将“url”包名与`url`参数重名了。请将后者改名。
  58. <details>
  59. <summary>英文:</summary>
  60. You&#39;ve shadowed the &quot;url&quot; package name with the `url` argument in your function. Rename the latter.
  61. </details>

huangapple
  • 本文由 发表于 2016年2月11日 08:32:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/35328728.html
匿名

发表评论

匿名网友

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

确定