February 2014: Interfaces & Iterators
Please RSVP. Pizza to be provided by Jobber.
Talks
Duck Hunt: Interfaces
@nathany explored interfaces through a series of small "duck" examples. Starting with a duck and a dog that share a Speak() method, the talk showed that simply matching a method set is enough to satisfy an interface — there is no implements keyword — and that a Speaker interface lets code work with any value that quacks without caring about its concrete type. Later examples demonstrated structural typing across package boundaries, type assertions that inspect the concrete type at runtime, and the empty interface (interface{}) that can hold any value. A linked-list stack example stored interface{} values and used a type switch to recover and act on each element's actual type.
Key points:
- Interfaces are satisfied implicitly — matching behavior (the method set) is enough.
- "If it quacks like a duck" —
IsDuckjust callsSpeak(). - Interfaces work across package boundaries through structural typing.
- Unrelated types can satisfy the same interface.
- Type assertions recover the concrete type at runtime.
- The empty interface
interface{}holds any value. - Use a type switch to branch on the dynamic type.
This talk builds on last month's talk on types & methods. If you missed it, you may wish to watch the screencast.
Iteration in Go
Abram Hindle surveyed the many ways Go lets you repeat work: for loops (both while-style and the three-clause form), range over slices and maps, recursion, and callback tree walkers. He then built up the functional patterns map and reduce, showing how small helper functions compose — and lamenting the lack of generics, which forces a separate implementation per element type. The talk argued that reducing dependencies between iterations is what makes them parallelizable, and demonstrated parallelIntIntMap and parallelIntIntMapReduce using a fixed pool of worker goroutines and channels to split the work. It finished with channel-based iterators, object-style iterators using Next()/Value(), and control-flow labels, break/continue/goto.
Key points:
- Go's iteration toolkit:
for,range, recursion, and callbacks. rangeover slices/maps yields index/key plus value.mapandreduceare easy to write, but no generics means duplication.- Reducing inter-iteration dependencies is the key to parallelism.
- Split work across a fixed worker pool with channels (fan-out/fan-in).
- Channel iterators expose a sequence as a range-able channel.
- Object iterators use
Next()/Value(); labels andgotoexist but are rarely needed.
@abramh (video, code & slides)
News & Events
- Drone and Docker, Open Source CI
- Go Style Guide
- Toward Go 1.3 (video, slides) feature freeze is March 1st
- Golang Weekly
GopherCon April 24-26 in Denver $299 USD (soldout)- dotGo October 10 in Paris 79 €
- Edmonton Go Meetup - March (propose a talk)