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 TestGet(t *testing.T) { comic, err := xkcd.Get(221) if err != nil { t.Fatal(err) } expected := &xkcd.Comic{ Num: 221, Title: "Random Number", SafeTitle: "Random Number", Img: "http://imgs.xkcd.com/comics/random_number.png", Alt: "RFC 1149.5 specifies 4 as the standard IEEE-vetted random number.", Year: "2007", Month: "2", Day: "9", News: "", Link: "", Transcript: `int getRandomNumber() { return 4; chosen by fair dice roll. guarenteed to be random. } {{title text: RFC 1149.5 specifies 4 as the standard IEEE-vetted random number.}}`, } t.Log("comic: ", comic) t.Log("expected: ", expected) if *comic != *expected { t.Fail() } }
func ExampleGet() { comic, err := xkcd.Get(140) if err != nil { log.Fatal(err) } fmt.Printf("Number: %v\n", comic.Num) fmt.Printf("Image: %v\n", comic.Img) fmt.Printf("Alt Text: %v\n", comic.Alt) // Output: // Number: 140 // Image: http://imgs.xkcd.com/comics/delicious.png // Alt Text: I'm currently in the I Have Cheese phase of this cycle. }
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) }
func Test404(t *testing.T) { _, err := xkcd.Get(404) if err.Error() != "404 Not Found" { t.Fatal(err) } }