Finally – An elegant solutions to generics in Go!

Finally – An elegant solutions to generics in Go!

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.

go get github.com/StabbyCutyou/generics

Now we can import the package into our Go program.

import . "github.com/StabbyCutyou/generics"

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.

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

with the G package now becomes the following

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

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.

LEAVE A COMMENT

1 comment
  1. Axel Wagner
    October 5, 2017 12:51:pm Reply

    Really, it should use type aliases. Otherwise it’d clash when you try to use e.g. `func(int)` as a `func(G)`.