Giter VIP home page Giter VIP logo

go-reddit's Introduction

go-reddit

Actions Status Go Report Card

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/[email protected]

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() {
    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{}.

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.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.

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.

go-reddit's People

Contributors

vartanbeno avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.