Skip to content

Instantly share code, notes, and snippets.

@humbhenri
Created January 5, 2014 20:29
Show Gist options
  • Save humbhenri/8273431 to your computer and use it in GitHub Desktop.
Save humbhenri/8273431 to your computer and use it in GitHub Desktop.
List reddit entries in golang
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
)
type RedditEntry struct {
Data struct {
Children []struct {
Data struct {
Domain string
Subreddit string
Url string
Title string
}
}
}
}
func (reddit *RedditEntry) List() {
for _, entry := range reddit.Data.Children {
fmt.Println(entry.Data.Title, "["+entry.Data.Url+"]", "\n")
}
}
const (
REDDIT = "http://www.reddit.com"
)
func err(e error) {
if e != nil {
panic(e)
}
}
func main() {
var subreddit string
var listing string
flag.StringVar(&subreddit, "sub", "", "The subreddit")
flag.StringVar(&listing, "list", "new", "Kind of listing: how, new, random, controversial, top")
flag.Parse()
var request string
if len(subreddit) > 0 {
request = REDDIT + "/r/" + subreddit + "/" + listing + ".json"
} else {
request = REDDIT + "/" + listing + ".json"
}
resp, e := http.Get(request)
err(e)
defer resp.Body.Close()
body, e := ioutil.ReadAll(resp.Body)
err(e)
var reddit RedditEntry
e = json.Unmarshal(body, &reddit)
err(e)
reddit.List()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment