GopherCon 2019 - Generics in Go

conference, golang, gophercon2019, notes

These are some notes from my experiences at the GopherCon 2019. I don’t expect these will be laid out in any particularly useful way; I am mostly taking them so I can remember some of the bits I found most useful in the future.


  • different functions
  • interface types
  • code generation

None are great

Real Generics

  • factor types out of code (not full c++ templating)
  • enable sets and other interesting data structures

Implementation Goals

  • minimize new concepts
  • complexity falls on the writer of generic code, not the user
  • writer and user of generic code can work independently
  • short build times, fast execution times
  • preserve clarity and simplicity of Go

Draft Design

  • define as func Reverse (type Element) (s []Element) {...}

  • call with Reverse(int)(s), or even Reverse(s) due to compiler help

  • in the example, type Element is the generic case (nothing special about the type)

    • Element is the name of the type param
  • another example might be type T Sequence

    • Sequence is a contract
    • contract Sequence(T) { T string, []byte }
    • can also list required methods
  • another example type T Stringer

    • contract Stringer(T) { T String() string }
    • like fmt.Stringer, but this doesn’t force the slice to be a slice of interface-typed values (still concrete)
  • type Graph (type Node, Edge G) struct { ... }

    • contract G(Node, Edge) { ... }
  • func Min (type T Ordered) (a, b T) T { ... }

    • contract Ordered(T) { T ... } (list of all built-in ordered types, not defining notation for operators)
    • only built-in types support operators, so this is pretty fine