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

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)

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)

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.

Tags:

Go