Doubt Function / Callback Golang

Asked

Viewed 36 times

0

Do I necessarily need to deal with Sponse for the function to be executed? As it stands it says that the variable "tweet" was declared but not used, but as it is a POST request, I see no point in dealing with the answer.

  //tokens auth
    httpClient := config.Client(oauth1.NoContext, token)
    api := twitter.NewClient(httpClient)
    tweet, resp, err := api.Statuses.Update("tweet teste", nil)
  //fim

In the documentation there are no practical examples, only modes of use.

https://pkg.go.dev/github.com/dghubble/go-twitter/twitter#Statusservice.Update

1 answer

1


In general, it makes sense to treat the answer. But if you don’t want to use the _.

That is, considering it has a function:

func SuaFuncao() (string, int, error) {
return "texto", 42, nil
}

This function returns three values, as well as the api.Statuses.Update. The order is always equal ((string, int, error)). Typically you would do:

resp, code, err := SuaFuncao()
if err != nil {
return err
}

However, if you don’t want to use the resp nor the code, swap for _:

_, _, err := SuaFuncao()
if err != nil {
return err
}

This will be enough to just get the err and ignore the other 2 values.


This is very common to use in loops, for example:

for _, v := range xxxxx {
}

The _ ignores the index of range. So you can always use this in any context.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.