75

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)
    ====>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
}
3
  • the star * is not needed perhaps?
    – akonsu
    Jan 2, 2014 at 20:09
  • if i remove it, then AuthorizeToken from TwitterApi starts complaining that it needs a pointer :) Jan 2, 2014 at 20:10
  • maybe TwitterApi(&requestToken,...)
    – akonsu
    Jan 2, 2014 at 20:14

3 Answers 3

124

This line:

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

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 (&), yielding a pointer-to-RequestToken, *oauth.RequestToken:

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

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:""}

b, err := TwitterApi(&requestToken, req.Oauth_verifier)
0
11

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

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

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:""}

Now requestToken is a oauth.RequestToken value.

Then you can pass a pointer to that value to TwitterApi

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

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

*&yourStruct

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

2
  • didn't know you can do that :) Although I do think you don't going to see this in code because the above would be the same as just yourStruct example play.golang.org/p/DeIQ23Z4KqJ Mar 24, 2019 at 15:53
  • your code just mean, value referenced on address of variable yourStruct, so it wil just the same writing yourStruct
    – rizesky
    Jul 11, 2020 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.