Back to Gotraining

Part 1: Just Get it Working

topics/go/exercises/contributors/README.md

latest2.6 KB
Original Source

GitHub Client Exercise

Create a project folder in your $GOPATH outside of this repository. A good place would be $GOPATH/src/github.com/yourname/contributors where yourname is your GitHub username.

Part 1: Just Get it Working

Create a program which can call the GitHub API to get a list of contributors for the golang/go repository.

A template file is included to get you started.

Part 2: Refactoring

  • Refactor your GitHub API client to a type called Client with a method Contributors.
  • Move your client code from package main to another package like package github.
  • Create a function in package main that uses the client. func printContributors(repo string, c *github.Client) int
  • Decouple the printContributors function to depend on an interface instead of the concrete type.

Part 3: Testing

  • Add tests for printContributors. Create a mock version of the github client and pass that in. To capture the output you can change the printContributors function to accept an io.Writer where it should print results.
  • Add tests for the github package using net/http/httptest.NewServer.

Just for Fun

  • Add flags to the main package for specifying the repo to pull. Use the flag package.
  • Add a flag to the main package to specify an output file name then encode the results to that file in CSV format. Use the encoding/csv package.
  • Create a web app that accepts a repo name then shows the contributor list for that repo