go-xkcd: github.com/nishanths/go-xkcd Index | Files

package xkcd

import "github.com/nishanths/go-xkcd"

package xkcd provides methods to make requests to the xkcd.com API.


client := xkcd.NewClient()
comic, err := client.Latest()

if err != nil {

fmt.Printf("%+v", comic)

All functions on Client are safe to use concurrently. The Random() and RandomInRange() functions do not modify the global random number generator.

More details on the xkcd API can be found at https://xkcd.com/json.html.


Package Files

client.go comics.go config.go doc.go errors.go random.go

type Client Uses

type Client struct {
    HTTPClient *http.Client

Client represents the HTTP client and any settings used to make requests to the xkcd API.

func NewClient Uses

func NewClient() *Client

NewClient returns a Client configured with sane default values.

func (*Client) Get Uses

func (c *Client) Get(number int) (Comic, error)

Get returns the comic for the specified number. The number is the comic number in the xkcd.com url. For example: https://xkcd.com/193.

func (*Client) Latest Uses

func (c *Client) Latest() (Comic, error)

Latest returns the latest comic's information

func (*Client) Random Uses

func (c *Client) Random() (Comic, error)

Random returns a random comic. The underlying random number generator's behavior may not match the behavior of the Random button on xkcd.com. Random never performs a request for a non-existent comic number.

Also see: RandomInRange()

func (*Client) RandomInRange Uses

func (c *Client) RandomInRange(begin, end, latest int) (comic Comic, err error)

RandomInRange returns a random comic using the given options. The underlying random number generator's behavior may not match the behavior of the Random button on xkcd.com.

[begin, end) specify the range that the randomly chosen comic number can be in. If begin equals -1, begin defaults to the number of the first comic. Likewise, if end equals -1, end defaults to the number of the latest comic + 1.

latest specfies the number of the latest xkcd comic. Specifying the number eliminates the overhead of performing an additional HTTP request to find this number. Pass in -1 to let RandomInRange() find the latest comic number by performing the additional request.

type Comic Uses

type Comic struct {
    Alt         string
    PublishDate time.Time
    ImageURL    string
    URL         string
    News        string
    Number      int
    SafeTitle   string
    Title       string
    Transcript  string

Comic represents information and metadata about a single xkcd comic.

func (*Comic) UnmarshalJSON Uses

func (comic *Comic) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the response from the xkcd enpoint

type Config Uses

type Config struct {
    UseHTTPS bool

Config represents the settings that a Client will use when making requests.

type StatusError Uses

type StatusError struct {
    StatusCode int
    StatusText string

StatusError specifies the status code and status text for error responses from the xkcd API endpoint.

func (StatusError) Error Uses

func (e StatusError) Error() string

Error returns a string representation of the StatusError.

Package xkcd imports 8 packages (graph) and is imported by 3 packages. Updated 2017-04-19. Refresh now. Tools for package owners.