doucong7963 2019-04-11 19:09
浏览 66
已采纳

从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 2019-04-11 20:05
    关注

    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..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退