Go library for accessing the Reddit API.
Go to file
Vartan Benohanian 2f1019d170 Update logo
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
2021-02-04 19:26:35 -05:00
.github/workflows Unexport post/comment service. Rename github workflow 2020-08-02 19:04:53 -04:00
examples Bump version to v2 2021-01-31 20:36:04 -05:00
images Update logo 2021-02-04 19:26:35 -05:00
reddit Edit change log, move it to root directory of repo 2021-01-24 23:03:51 -05:00
testdata Create more widget types 2020-09-25 10:38:58 -04:00
.gitignore Initial commit 2020-04-23 20:10:02 -04:00
CHANGELOG.md Update readme and changelog 2021-01-31 21:25:37 -05:00
go.mod Update module path in go.mod 2021-01-31 20:10:48 -05:00
go.sum Set HTTP client via option. Update readme, Makefile, go.sum 2020-08-26 23:13:34 -04:00
LICENSE Update README.md and license. Add examples 2020-08-02 15:59:25 -04:00
Makefile Set HTTP client via option. Update readme, Makefile, go.sum 2020-08-26 23:13:34 -04:00
README.md Update readme 2021-02-01 12:09:45 -05:00


go-reddit logo

Actions Status Go Report Card PkgGoDev

Overview

Featured in issues 327 and 347 of Golang Weekly 🎉

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/v2@vX.Y.Z

Or for the latest version:

go get github.com/vartanbeno/go-reddit/v2

The repository structure for managing multiple major versions follows the one outlined here.

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/v2/reddit"

func main() {
    credentials := reddit.Credentials{ID: "id", Secret: "secret", Username: "username", Password: "password"}
    client, _ := reddit.NewClient(credentials)
}

You can pass in a number of options to NewClient to further configure the client (see reddit/reddit-options.go). For example, to use a custom HTTP client:

httpClient := &http.Client{Timeout: time.Second * 30}
client, _ := reddit.NewClient(credentials, reddit.WithHTTPClient(httpClient))

Read-Only Mode

The DefaultClient method returns a valid, read-only client with limited access to the Reddit API, much like a logged out user. You can initialize your own and configure it further using options via NewReadonlyClient:

client, _ := reddit.NewReadonlyClient()

Examples

Configure the client from environment variables. When using this option, it's ok to pass an empty struct for the credentials.
client, _ := reddit.NewClient(reddit.Credentials{}, reddit.FromEnv)
Submit a comment.
comment, _, err := client.Comment.Submit(context.Background(), "t3_postid", "comment body")
if err != nil {
    return err
}
fmt.Printf("Comment permalink: %s\n", comment.Permalink)
Upvote a post.
_, err := client.Post.Upvote(context.Background(), "t3_postid")
if err != nil {
    return err
}
Get r/golang's top 5 posts of all time.
posts, _, err := client.Subreddit.TopPosts(context.Background(), "golang", &reddit.ListPostOptions{
    ListOptions: reddit.ListOptions{
        Limit: 5,
    },
    Time: "all",
})
if err != nil {
    return err
}
fmt.Printf("Received %d posts.\n", len(posts))

More examples are available in the examples folder.

Design

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

Contributing

Contributions are welcome! For any bug reports/feature requests, feel free to open an issue or submit a pull request.

License

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