Go generics beyond the playground

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • SaaSHub - Software Alternatives and Reviews
  • go

    The Go programming language

    While Go as of version 1.16 does not support Generics, there is an accepted language proposal for it, and we can probably expect it to arrive in Go 1.18 or later. But as it turns out, we don't need to wait for it to start experimenting with the new feature. We can start now to write small snippets of code on the go2go playground, or even full Go packages that you develop locally.

  • subx

    An experimental test library written with the go generics experiment.

    In this article, I will go through how I re-wrote a test matching library from scratch with generics as part of the tool-box. My hope is that this will inspire you to do your own experiments with Go generics beyond the playground and write something potentially useful. Only then, can we truly see if generics itself, is going to be useful in Go. If you want, you could use the library in this article for testing your experiments; or you can extend the library and do a pull request.

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

  • subtest

    A utility for generating runnable sub-tests in Go

    Starting out with Go generics, in order to do something useful, we need a problem to solve. The problem I have picked for this article is one that I have tried to solve before when designing the test matcher/assertion library that we use to test the Clarify back-end at Searis. But first, you probably have a question: with all the great test matcher libraries we have in Go, why on earth would we want to write a new one? To answer that, it's worth having a closer look at at one of the existing matcher libraries. Does it solve it's mission in a useful way, and with the best possible package design?

  • gomega

    Ginkgo's Preferred Matcher Library

    If we do the count, we gather that subtest appear to solve five out of the six problems we identified with the assert library. At this point though, it's important to note that at the time when the assert package was designed, the sub-test feature in Go did not yet exist. Therefore it would have been impossible for that library to embed it into it's design. This is also true for when Gomega and Ginko where designed. If these test frameworks where created now, then most likely some parts of their design would have been done differently. What I am trying to say is that with even the slightest change in the Go language and standard library, completely new ways of designing programs become possible. Especially for new packages without any legacy use-cases to consider. And this brings us to generics.

  • Testify

    A toolkit with common assertions and mocks that plays nicely with the standard library

    In the Go world, by far the most popular Go matcher library still appear to be the assert package from the stretchr/testify repo. It's an old, good and stable library. However, because it's old, and because most (old) Go libraries strived to keep full backwards compatibility, it's also an excellent library to demonstrate some problems that can perhaps be solved by a different design. For this article, we will be content with considering the following code, testing an imaginary function mypkg.Sum:

  • expect

    A simple assertion library that you probably shouldn't use. (by pkg)

    We have to pass in the t parameter. It's a minor inconvenience, but enough for Dave Cheney to write a quite interesting package for solving it. Not recommended for production use, I might add.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts