Go library for accessing the Reddit API.
Go to file
Vartan Benohanian 49fa672619 Streaming new posts from subreddits
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
2020-08-22 00:40:34 -04:00
.github/workflows Unexport post/comment service. Rename github workflow 2020-08-02 19:04:53 -04:00
examples Move everything to new reddit/ folder 2020-08-20 14:37:59 -04:00
reddit Streaming new posts from subreddits 2020-08-22 00:40:34 -04:00
testdata Add tests 2020-08-18 23:12:35 -04:00
.gitignore
go.mod
go.sum
LICENSE
Makefile
README.md Move everything to new reddit/ folder 2020-08-20 14:37:59 -04:00

go-reddit

Actions Status

go-reddit is a Go client library for accessing the Reddit API.

You can view Reddit's official API documentation here.

Install

To get a specific version from the list of versions:

go get github.com/vartanbeno/go-reddit@vX.Y.Z

Or for the latest version:

go get github.com/vartanbeno/go-reddit

Usage

Make sure to have a Reddit app with a valid client id and secret. Here is a quick guide on how to create an app and get credentials.

package main

import "github.com/vartanbeno/go-reddit/reddit"

func main() {
    withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
    client, _ := reddit.NewClient(nil, withCredentials)
}

The first argument (the one set to nil) is of type *http.Client. It will be used to make the requests. If nil, it will be set to &http.Client{}.

The WithCredentials option sets the credentials used to make requests to the Reddit API.

Examples

Configure the client from environment variables.
client, _ := reddit.NewClient(nil, reddit.FromEnv)
Upvote a post.
_, err := client.Post.Upvote(context.Background(), "t3_postid")
if err != nil {
    fmt.Printf("Something bad happened: %v\n", err)
    return err
}
Get r/golang's top 5 posts of all time.
result, _, err := client.Subreddit.Top(context.Background(), "golang", &reddit.ListPostOptions{
    ListOptions: reddit.ListOptions{
        Limit: 5,
    },
    Time: "all",
})
if err != nil {
    fmt.Printf("Something bad happened: %v\n", err)
    return err
}
fmt.Printf("Received %d posts.\n", len(result.Posts))

More examples are available in the examples folder.

Design

The package design and structure are heavily inspired from Google's GitHub API client and DigitalOcean's API client.

License

This project is licensed under the MIT License - see the LICENSE file for details.