func main() { flag.Parse() if flag.NArg() != 0 { flag.Usage() os.Exit(1) } var comic *xkcd.Comic var err error if *number == 0 { comic, err = xkcd.GetCurrent() } else { comic, err = xkcd.Get(*number) } if err != nil { log.Fatal(err) } resp, err := http.Get(comic.Img) if err != nil { log.Fatal(err) } defer resp.Body.Close() _, err = io.Copy(os.Stdout, resp.Body) if err != nil { log.Fatal(err) } }
func TestNew(t *testing.T) { comic1, err := xkcd.GetCurrent() if err != nil { t.Fatal(err) } r, w := io.Pipe() go func() { e := json.NewEncoder(w) err = e.Encode(comic1) if err != nil { t.Fatal(err) } }() comic2, err := xkcd.New(r) if err != nil { t.Fatal(err) } t.Log("comic1: ", comic1) t.Log("comic2: ", comic2) if *comic1 != *comic2 { t.Fatal("comic1 and comic2 don't match") } }
func TestGetCurrent(t *testing.T) { comic1, err := xkcd.GetCurrent() if err != nil { t.Fatal(err) } comic2, err := xkcd.Get(comic1.Num) if err != nil { t.Fatal(err) } t.Log("comic1: ", comic1) t.Log("comic2: ", comic2) if *comic1 != *comic2 { t.Fatal("comic1 and comic2 don't match") } }
func main() { flag.Parse() if flag.NArg() != 0 { flag.Usage() os.Exit(1) } var comic *xkcd.Comic var err error if *number == 0 { comic, err = xkcd.GetCurrent() } else { comic, err = xkcd.Get(*number) } if err != nil { log.Fatal(err) } printInfo(comic) }