Talking to HTTP/JSON APIs with Go

less than 1 minute read

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 with ioutil.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 a http.HandlerFunc that serves test data on request.
  • Table-driven tests are awesome!
  • Sometimes GitHub is down.

Updated: