May 2017: Generation dep

Meetup #39. Please RSVP at Meetup.

We get started at 6:30 pm.

Sponsors

Venue provided by Startup Edmonton.

Food sponsored by Bellstone Engineering.

Talks

Code Generation

Gerrit Renker gave a tour of code generation, starting with how other languages have done it — Lisp macros, Perl, Ruby, and Python's Jinja2 templates — before focusing on Go. In Go the usual route is go generate plus a generator, and the talk walked through tools like gocog, goa for micro-service APIs, go-swagger driven by OpenAPI, stringer for fmt.Stringer implementations, and enumer for enum Stringer/JSON methods. The tip of the month was easygen, which combines YAML input with text/template/html/template to emit code, and the talk showed how go generate discovers generators through //go:generate comments and the $GOFILE/$GOPACKAGE/$GOLINE environment variables.

Key points:

  • Code generation is an old idea — Lisp's defmacro is code that writes code.
  • Go's go generate runs generators declared in //go:generate comments.
  • stringer, jsonenums, and enumer generate Stringer and JSON methods for enums.
  • goa and go-swagger generate servers/clients from API DSLs and OpenAPI specs.
  • easygen renders YAML through text/template/html/template.
  • Generators receive $GOFILE, $GOPACKAGE, and $GOLINE.

Gerrit Renker (slides)

dep

Nathan Youngman introduced dep, Go's then-experimental dependency management tool, by first comparing it to the package managers other languages already had — pip, Bundler, npm, Maven, Composer, and Cargo — which let you decide when to upgrade, lock versions when things break, and opt into pre-releases. He traced Go's history from go get (2012), which fetches repositories by import path, to the vendor directory (2015), which copies third-party code into a project for a hermetic view of the world and protection from dependencies that move or disappear. dep init generates Gopkg.toml and Gopkg.lock; dep traverses your source for imports, still uses the vendor folder, and at the time had no central registry or flat namespace.

Key points:

  • Other languages' package managers let you time upgrades and lock versions.
  • go get fetches packages straight from repositories by import path.
  • The vendor folder copies dependencies in, protecting against disappearing packages.
  • dep init creates Gopkg.toml (manifest) and Gopkg.lock (lock file).
  • dep scans source imports and keeps using vendor/.
  • No central registry or flat namespace in the early dep design.

Nathan Youngman (slides)

Activities

  • Kick the tires on dep, now that the manifest files are considered stable. Bring your laptop and a Go project if you have one.

Events