英文:
How to properly pass arguments in Go function?
问题
这是来自Anaconda Go包的一个函数:
func (a TwitterApi) GetHomeTimeline(v url.Values) (timeline []Tweet, err error) {
if v == nil {
v = url.Values{}
}
if val := v.Get("include_entities"); val == "" {
v.Set("include_entities", "true")
}
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/statuses/home_timeline.json", v, &timeline, _GET, response_ch}
return timeline, (<-response_ch).err
}
当我像这样调用函数时,它可以正常工作:
result, _ := api.GetHomeTimeline(nil)
但现在我想将参数从nil
更改为其他值。在示例*中,它被设置为:
v := url.Values{}
v.Set("count", "30")
result, _ := api.GetHomeTimeline(v)
但这会生成错误:undefined: url in url.Values
*
- 我已将示例设置为与我的请求匹配。
英文:
This is a function from the Anaconda Go package:
func (a TwitterApi) GetHomeTimeline(v url.Values) (timeline []Tweet, err error) {
if v == nil {
v = url.Values{}
}
if val := v.Get("include_entities"); val == "" {
v.Set("include_entities", "true")
}
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/statuses/home_timeline.json", v, &timeline, _GET, response_ch}
return timeline, (<-response_ch).err
}
When I call the function like this, it works fine:
result, _ := api.GetHomeTimeline(nil)
But now I want to change the argument from nil
into something else. In the example* this is set as:
v := url.Values{}
v.Set("count", "30")
result, _ := api.GetHomeTimeline(v)
But this generates the error: undefined: url in url.Values
*
- I've set the example to match my request.
答案1
得分: 3
根据评论中的报道,如果其他人正在寻找答案,上述代码中没有错误,只是缺少了一个导入语句:
import "net/url"
英文:
As reported in the comment, if someone else is looking for the answer, there is no mistake in the code above, just a missing import:
import "net/url"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论