Talking to HTTP/JSON APIs with Go
Slides
Yesterday I gave a presentation at the Go User Group Hamburg called “Talking to HTTP/JSON APIs with Go”. As promised, I’ve published my slides today. Unfortunately, talks.godoc.org does not allow running Go code embedded in slides, but I’m a kind person and have released the slide sources as well. (If you’re curious, I’ve used the very simple and very cool present format.)
Takeaways
- Download JSON data via HTTP using
http.Get
. Read the HTTP body withioutil.ReadAll
. - Use
json.Unmarshal
to decode JSON data into Go structs (with optional struct tags). - For unit testing, start an internal web server with
httptest.NewServer
and provide ahttp.HandlerFunc
that serves test data on request. - Table-driven tests are awesome!
- Sometimes GitHub is down.
Links
- GitHub System Status API
- go-ghstatus - Go library for GitHub’s system status API
- Mentioned Go packages: http, httptest, and json
- Articles: JSON and Go and Writing Web Applications