Interconnect. Smarter.


Recent Posts

Go Generics

Generics are the most important new feature of the 1.18 version of Go that was just released. I offer you a quick tour of this new feature in this article.

Before Go 1.18

It has always been possible to produce generic code with Go using interface{} type. For instance, you write a function that prints n times given value with:

On the Playground

This example is very simple because function fmt.Println() accepts any type. Before Go 1.18, its signature was func Println(a ...interface{}) (n int, err error).

Furthermore, one can define an argument type with a specific interface. For instance:

On the Playground

Type error is an interface that defines a single method Error() string. Thus you can send anything to function PrintError() provided it implements method  Error().

The beginning of troubles

Let’s suppose we want to write a function that returns the maximum of given values. We could write, for integers, following code:

On the Playground

If we want to generalize this function for other types, interfaces are not of any help because no function can define comparison operators. Thus we have to write this function for all types! It would be possible to accept type interface{}, but we would have to do type assertions and this would not simplify things.

Generics to the rescue

Go 1.18 implements Generics. We can now add type parameters in function signature. To make our Max() function generic, we could write

On the Playground

This way, with type parameter [N int | float64], we indicate that function parameters may be of type int or float64. Note that we can’t mix types, thus call Max(1, 2.0) would not compile.

Interfaces strike back

With Go 1.18, we can now define interfaces as a list of types. We could write example above as follows:

On the Playground

Type aliases

If we define an alias for given type, we can include it in an interface with ~ character, as follows:

On the Playground

Thus ~int includes type int but also all its aliases, and thus also Num.


It can be very tedious to define your own interfaces with type lists. Package provides following interfaces:

-Signed : ~int | ~int8 | ~int16 | ~int32 | ~int64
-Unsigned : ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
-Integer : Signed | Unsigned
-Float : ~float32 | ~float64
-Ordered : Integer | Float | ~string
-Complex : ~complex64 | ~complex128

We could now use constraint constraints.Ordered as follows:

On the Playground


It is possible to pass type arguments while calling a generic function. For instance:

Expression Max[int] is an instantiation of generic function Max. It defines types for parameters. We could write:

Function MaxFloat is now a non generic function that accepts only float arguments.

Types with type parameters

Let’s say we want to compute the sum of all elements in a given list. With standard Go linked lists, we could write:

On the Playground

This doesn’t compile because we can’t add interface{} types, which is type for list elements value: src/list.go:12:3: invalid operation: sum += e.Value (mismatched types int and any). We can notice that any is the new name for interface{} type.

Using type interface{} or any is boring because we must cast values to use them. Of course there is a Generics based solution. Here is a minimalist implementation of linked list with Generics:

On the Playground

In this code we added type parameters to type definitions, as in Element[T any]. This notation indicates that we define type Element that contains type T that may be anything. We don’t have to cast values to use them.

It is important to note that we set list type on instanciation:

This way we tell compiler that our list contains int and we now can use them as integers.

Type inference

We saw that we can set type parameters while calling a generic function with:

In this case, compiler knows parameters types because we tell it. But when we write:

In this case compiler infers parameters type of the generic function from argument’s type while performing call. This inference type is called function argument type inference. Nevertheless, it is sometimes impossible to infer types for return values, as in this example:

We must then help compiler instantiating function before calling it:


Generics are the new big thing in Go 1.18 which is the most important release since Go was Open Sourced. Nevertheless, this feature was not heavily tested on production and thus should be used with care, and of course widely tested.

InterCloud Team

Cloud innovation brings the IT agility digital businesses need to grow and scale. At InterCloud, we believe it is high time for cloud networking to deliver on this promise as well. The massive migration of IT resources out of corporate data centers to the Cloud paved the way for new IT production methods.

AWS & InterCloud

Your Comments :


Read more of what you like.