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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值