doucong7963
doucong7963
2019-04-11 19:09

从Twitter库搜索中获取数据到Go中的结构

已采纳

How do I append output from a twitter search to the field Data in the SearchTwitterOutput{} struct.

Thanks!

I am using a twitter library to search twitter base on a query input. The search returns an array of strings(I believe), I am able to fmt.println the data but I need the data as a struct.

type SearchTwitterOutput struct {
    Data  string
}

func (SearchTwitter) execute(input SearchTwitterInput) (*SearchTwitterOutput, error) {

    credentials := Credentials{
        AccessToken:       input.AccessToken,
        AccessTokenSecret: input.AccessTokenSecret,
        ConsumerKey:       input.ConsumerKey,
        ConsumerSecret:    input.ConsumerSecret,
    }

    client, err := GetUserClient(&credentials)
    if err != nil {
        return nil, err
    }

    // search through the tweet and returns a
    search, _ , err := client.Search.Tweets(&twitter.SearchTweetParams{
        Query: input.Text,
    })
    if err != nil {
        println("PANIC")
        panic(err.Error())
        return &SearchTwitterOutput{}, err
    }

    for k, v := range search.Statuses {
        fmt.Printf("Tweet %d - %s
", k, v.Text)
    }

    return &SearchTwitterOutput{
        Data: "test", //data is a string for now it can be anything
    }, nil
}

 //Data field is a string type for now it can be anything
//I use "test" as a placeholder, bc IDK...

Result from fmt.Printf("Tweet %d - %s
", k, v.Text):
Tweet 0 - You know I had to do it to them! @JennaJulien @Jenna_Marbles @juliensolomita @notjulen Got my first hydroflask ever…
Tweet 1 - RT @brenna_hinshaw: I was in J2 today and watched someone fill their hydroflask with vanilla soft serve... what starts here changes the wor…
Tweet 2 - I miss my hydroflask :(

This is my second week working with go and new to development. Any help would be great.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dsa89029 dsa89029 2年前

    It doesn't look like the client is just returning you a slice of strings. The range syntax you're using (for k, v := range search.Statuses) returns two values for each iteration, the index in the slice (in this case k), and the object from the slice (in this case v). I don't know the type of search.Statuses - but I know that strings don't have a .Text field or method, which is how you're printing v currently.

    To your question:

    Is there any particular reason to return just a single struct with a Data field rather than directly returning the output of the twitter client?

    Your function signature could look like this instead:

    func (SearchTwitter) execute(input SearchTwitterInput) ([]<client response struct>, error)

    And then you could operate on the text in those objects in wherever this function was called.

    If you're dead-set on placing the data in your own struct, you could return a slice of them ([]*SearchTwitterOutput), in which case you could build a single SearchTwitterOutput in the for loop you're currently printing the tweets in and append it to the output list. That might look like this:

        var output []*SearchTwitterOutput
        for k, v := range search.Statuses {
            fmt.Printf("Tweet %d - %s
    ", k, v.Text)
            output = append(output, &SearchTwitterOutput{
                Data: v.Text,
                })
        }
    
        return output, nil
    

    But if your goal really is to return all of the results concatenated together and placed inside a single struct, I would suggest building a slice of strings (containing the text you want), and then joining them with the delimiter of your choosing. Then you could place the single output string in your return object, which might look something like this:

        var outputStrings []string
        for k, v := range search.Statuses {
            fmt.Printf("Tweet %d - %s
    ", k, v.Text)
            outputStrings = append(outputStrings, v.Text)
        }
        output = strings.Join(outputStrings, ",")
    
        return &SearchTwitterOutput{
            Data: output,
        }, nil
    

    Though I would caution, it might be tricky to find a delimiter that will never show up in a tweet..

    点赞 评论 复制链接分享