go-reddit is a Go client library for accessing the Reddit API.
You can view Reddit's official API documentation here.
To get a specific version from the list of versions:
go get github.com/vartanbeno/[email protected]
Or for the latest version:
go get github.com/vartanbeno/go-reddit
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() {
credentials := &reddit.Credentials{
ID: "id",
Secret: "secret",
Username: "username",
Password: "password",
}
client, _ := reddit.NewClient(nil, credentials)
}
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{}
.
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.TopPosts(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.
The package design and structure are heavily inspired from Google's GitHub API client and DigitalOcean's API client.
This project is licensed under the MIT License - see the LICENSE file for details.