So hanging out at GothamGo this year has been inspirational! I have been able to rub elbows with the best of the best Go engineers in the world.

Last night I was introduced to what I think is..

..finally the solution to generics in Go!

The G Package

The Generics Package

So there is an Apache 2 open source licensed package that can be found on GitHub here. ‘

The package is clean, and elegant. So let’s take the package for a spin!

First things first, we need to install the G package. Luckily it using the Go idiomatic installation method go get.

[bash]

go get github.com/StabbyCutyou/generics

[/bash]

Now we can import the package into our Go program.

[bash]

import . "github.com/StabbyCutyou/generics"

[/bash]

The Implementation

We can now take ugly and non idiomatic (but flexible) Go code such as the following and implement a much more elegant solution for Generics.

[go]

func UglyUnIdiomaticQuoteGenericApproachUnquote(poorexcuse ...interface{}) []interface{}

[/go]

with the G package now becomes the following

[go]

func Excellence(things ...G) []G

[/go]

Backwards Compatibility

From the G package repository we can read a glorious compatibility statement:

G meets the standard of golang by matching its stance on backwards compatibility. Until a 2.0 release of generics, which may never happen, G will always be 100% Backwards compatible with it’s initial 1.0 release.

Behind The Scenes

The source code for G is simple and elegant, and I encourage all users to take a peak at what is going on behind the scenes. In my eyes it is a clean solution to Generics in Go, and I couldn’t be more pleased with the project.

I hope this helps.

Cheers.