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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,