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

package xkcd

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

Package xkcd provides a HTTP client for the xkcd.com JSON API.


c := xkcd.NewClient()

comic, err := c.Latest(context.Background())
if err != nil {
fmt.Printf("%+v\n", comic)

All methods on Client are safe to use concurrently.

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


Package Files

doc.go xkcd.go


const BaseURL = "https://xkcd.com"

BaseURL is the default base URL for the xkcd JSON API.

type Client Uses

type Client struct {
    HTTPClient *http.Client
    BaseURL    string

func NewClient Uses

func NewClient() *Client

NewClient constructs a client using http.DefaultClient and the default base URL. The returned client is ready for use.

func (*Client) Get Uses

func (c *Client) Get(ctx context.Context, number int) (Comic, error)

Get returns the xkcd comic for the given comic number.

func (*Client) Image Uses

func (c *Client) Image(ctx context.Context, number int) (io.Reader, string, error)

Image returns the image data for the given comic number and the value of the image response's Content-Type header.

func (*Client) Latest Uses

func (c *Client) Latest(ctx context.Context) (Comic, error)

Latest returns the latest xkcd comic.

type Comic Uses

type Comic struct {
    Alt        string
    Day        int
    ImageURL   string
    URL        string
    Month      int
    News       string
    Number     int
    SafeTitle  string
    Title      string
    Transcript string
    Year       int

Comic contains information about an xkcd comic.

type StatusError Uses

type StatusError struct {
    Code int

StatusError is returned when a bad response status code is received from the API.

func (StatusError) Error Uses

func (e StatusError) Error() string

Package xkcd imports 8 packages (graph) and is imported by 3 packages. Updated 2020-05-21. Refresh now. Tools for package owners.