How to properly pass arguments in Go function?

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

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(&quot;include_entities&quot;); val == &quot;&quot; {
		v.Set(&quot;include_entities&quot;, &quot;true&quot;)
	}

	response_ch := make(chan response)
	a.queryQueue &lt;- query{a.baseUrl + &quot;/statuses/home_timeline.json&quot;, v, &amp;timeline, _GET, response_ch}
	return timeline, (&lt;-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(&quot;count&quot;, &quot;30&quot;)
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 &quot;net/url&quot;

huangapple
  • 本文由 发表于 2016年1月5日 05:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/34600582.html
匿名

发表评论

匿名网友

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

确定