Consuming a REST API in Go
Go in 5 Minutes, episode 26.
In this screencast, we're going to build a command line client to consume the awesome Dark Sky API.
Instead of using an already-built Dark Sky API client (there are a few for Go), we're going to build our own client according to the API documentation to show some tips and tricks for building clients for any REST API.
In this screencast, we'll use the awesome gorequest package to help us build a DarkSky client from scratch.
- Quick primer on Cobra
- Quick primer on gorequest
- Let's check out the code!
How to Run This Code
You'll need Go version 1.11 or above to run this code. If you have an appropriate version, simply run
go build -o darksky . to build.
Before you run the binary, you'll need an environment variable called
DARKSKY_API_KEY set to your DarkSky API key (if you don't have one, get it from your account, or create an account if you haven't already).
Then, call the binary like so, ensuring that
DARKSKY_API_KEY is set in your environment:
$ ./darksky temp --lat 45.512230 --long -122.658722
long flags are set to the latitude and longitude (respectively) of the location for which to get the temperature.
The latitude and longitude in the above example are set to Portland, OR, USA. If you'd like to try another location, you can use https://www.latlong.net/