为什么*a{…}是无效的间接引用?

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

Why is *a{...} invalid indirect?

问题

以下是翻译的内容:

为什么下面这行代码是无效的?

func (s *Service) Callback(r *http.Request, req *RequestOauth, resp *Response) error {
    c := endpoints.NewContext(r)
    consumer.HttpClient=urlfetch.Client(c)
    ====>requestToken := *oauth.RequestToken{Token:req.Oauth_token, Secret:""}<======
    b, err := TwitterApi(requestToken, req.Oauth_verifier)
    resp.Message=b.Name
    return err
}

func TwitterApi(requestToken *oauth.RequestToken, verificationCode string) (u *UserT, err error) {
    accessToken, err := consumer.AuthorizeToken(requestToken, verificationCode)
    if err != nil {log.Fatal(err)}
    response, err := consumer.Get("https://api.twitter.com/1.1/account/verify_credentials.json", nil, accessToken)
    if err != nil {log.Fatal(err)}
    defer response.Body.Close()
    b, err := ioutil.ReadAll(response.Body)
    err = json.Unmarshal(b, &u)
    return
}

这行代码为什么是无效的?

英文:

invalid indirect of oauth.RequestToken literal (type oauth.RequestToken)

Why is the following line invalid?

func (s *Service) Callback(r *http.Request, req *RequestOauth, resp *Response) error {
	c := endpoints.NewContext(r)
	consumer.HttpClient=urlfetch.Client(c)
	====&gt;requestToken := *oauth.RequestToken{Token:req.Oauth_token, Secret:&quot;&quot;}&lt;======
	b, err := TwitterApi(requestToken, req.Oauth_verifier)
	resp.Message=b.Name
	return err
}

func TwitterApi(requestToken *oauth.RequestToken, verificationCode string) (u *UserT, err error) {
	accessToken, err := consumer.AuthorizeToken(requestToken, verificationCode)
	if err != nil {log.Fatal(err)}
	response, err := consumer.Get(&quot;https://api.twitter.com/1.1/account/verify_credentials.json&quot;, nil, accessToken)
	if err != nil {log.Fatal(err)}
	defer response.Body.Close()
	b, err := ioutil.ReadAll(response.Body)
	err = json.Unmarshal(b, &amp;u)
	return
}

答案1

得分: 122

这行代码的翻译如下:

requestToken := *oauth.RequestToken{Token:req.Oauth_token, Secret:""}

直译的意思是“创建一个 oauth.RequestToken 的实例,然后尝试将其作为指针进行解引用”。也就是说,它试图通过一个字面上的结构值来进行间接(指针)访问。

相反,你想要创建该实例并取得它的地址(&),从而得到一个指向 RequestToken 的指针,即 *oauth.RequestToken

requestToken := &oauth.RequestToken{Token:req.Oauth_token, Secret:""}

或者,你可以将令牌创建为一个局部值,然后通过地址传递给 TwitterApi 函数:

requestToken := oauth.RequestToken{Token:req.Oauth_token, Secret:""}

b, err := TwitterApi(&requestToken, req.Oauth_verifier)
英文:

This line:

requestToken := *oauth.RequestToken{Token:req.Oauth_token, Secret:&quot;&quot;}

translated literally says "create an instance of oauth.RequestToken, then attempt to dereference it as a pointer." i.e. it is attempting to perform an indirect (pointer) access via a literal struct value.

Instead, you want to create the instance and take its address (&amp;), yielding a pointer-to-RequestToken, *oauth.RequestToken:

requestToken := &amp;oauth.RequestToken{Token:req.Oauth_token, Secret:&quot;&quot;}

Alternatively, you could create the token as a local value, then pass it by address to the TwitterApi function:

requestToken := oauth.RequestToken{Token:req.Oauth_token, Secret:&quot;&quot;}

b, err := TwitterApi(&amp;requestToken, req.Oauth_verifier)

答案2

得分: 11

你需要创建一个指向你正在创建的值的指针,可以使用&来完成,*则相反,它解引用一个指针。所以:

requestToken := &oauth.RequestToken{Token:req.Oauth_token, Secret:""}

现在,requestToken是一个指向oauth.RequestToken值的指针。

或者你可以将requestToken初始化为一个值:

requestToken := oauth.RequestToken{Token:req.Oauth_token, Secret:""}

现在,requestToken是一个oauth.RequestToken值。

然后,你可以将指向该值的指针传递给TwitterApi:

b, err := TwitterApi(&requestToken, req.Oauth_verifier)
英文:

You'll need to create a pointer to the value you're creating, which is done with &amp; , * does the opposite, it dereferences a pointer. So:

requestToken := &amp;oauth.RequestToken{Token:req.Oauth_token, Secret:&quot;&quot;}

Now requestToken is a pointer to a oauth.RequestToken value.

Or you can initialize requestToken as a value:

requestToken := oauth.RequestToken{Token:req.Oauth_token, Secret:&quot;&quot;}

Now requestToken is a oauth.RequestToken value.

Then you can pass a pointer to that value to TwitterApi

  b, err := TwitterApi(&amp;requestToken, req.Oauth_verifier)

答案3

得分: 4

我可以为你提供翻译服务,以下是翻译好的内容:

如果我们想要在一行代码中明确查看结构体的值,可以这样做:

*&amp;yourStruct

在这里,你可以获取结构体的实例,查找其内存地址,并访问其值。

英文:

I may add to the top answer, if we want to explicitly look at a struct value in one line we could do this :

*&amp;yourStruct

Where you get the instance of your struct, look up at its memory address, and access its value.

huangapple
  • 本文由 发表于 2014年1月3日 04:01:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/20890850.html
匿名

发表评论

匿名网友

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

确定