Lately, Go has been getting a lot of flack for its lack of generics and parametric polymorphism. Whereas it doesn't have a specific language feature called "generics", generic type operations are very simple to implement. Here is a simple implementation of a generic collection type in Go demonstrating how it's done. Essentially, the type interface{}
acts as a placeholder for any type. By making use of the empty interface, you can pass any type to a function much like you can in dynamically-typed languages.
Edit: Thanks to https://gist.github.com/mfenniak and https://gist.github.com/dominikh for pointing out that the add() function was equivalent to go's built-in append()
@mfenniak append works just fine on nil slices, due to the simple fact that capacity for nil slices is defined as
0
.(On a side note, it would also make
add
a lot more efficient)