go1.18 Generics

The long awaited day of generics in go has finally come. As I’ve been writing go code for years without the need for generics, it is unluckily that generics will be part of the every day programming. In the early days we will see a lot of libraries and examples pop-up for them, but many of them may not be needed as most general use cases will probably be added into the standard library in future release of go. This example is a learning exercise to understand and use generics. – Official Tutorial from go team

Generic Min and Max functions

I’ve always been bothered by the need to cast an integer into a float and back again with working with the math.Min and math.Max functions.

Declaring the type constraint

The constraint tells our generic function what values or behaviors are allowed. We have a list of the some basic constraints already defined in that we can take advantage of.

  • Download the constraints package for use go get
type Number interface {
	constraints.Integer | constraints.Float

// alternatively without the constraints package we could have
type Numbers interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64

Defining generic functions

A generic function looks the same except for the type constraint declared after the function name and before the parameter list `[N Number]. The first parameter is the type name we can use to reference our generic type in the function, the second is the constraint interface we defined above. Multiple constraints can be defined with by a comma separated list.

func Min[N Number](x, y N) N {
	if x < y {
		return x
	return y

func Max[N Number](x, y N) N {
	if x > y {
		return x
	return y

Use the methods

Hooray, we can now use the Min and Max functions without type casting

func main() {
	fmt.Println("Min int:", Min(1, 5))
	fmt.Println("Min float:", Min(12.5, 5.7))

	fmt.Println("Max int:", Max(1, 5))
	fmt.Println("Min float:", Max(12.5, 5.7))


The go compile still enforces types with generics we can’t mix different types within a generic function. The return values will be the same type as used in the generic types.

// mixing floats and ints 
Min(1,10.4) // compile error 
Min(float64(1), 10.4)

// missing different int types 
Max(int8(1), int16(5)) // compile error 

var i int 
i = Max(12.5,7.55) // compile error can't set float to int

Gophercon 2021

Go at Mainframe Scale

Kaylyn Gibilterra talks about her journey of learning cobalt on Mainframes and insights into converting them into go.


Because go is a new language it is hard to find people that already have experience in it. but because of how it is written it can be easy for engineers from different backgrounds to pick up. This chart show where different engineers start with go and some of the troubles they have moving over.

Lightning Talks

gomponents – view components in pure go (like React)

chezmoi – Cross-platform dotfile manager

allows syncing config files between multiple systems including windows. has some common tools embedded in (curl, wget, diff, tar, git)

Kyoto frontend-like components – this looks like a very early project

TinyGo – embedded systems and WebAssembly

Zoekt – Optimizing Memory

Day 2

AI-Driven Development in Go

Queues, Fairness and The Go Scheduler

3D Printing Gophers with Go – printer hardware – track printer status – go CAD program which can be used to create stl files for 3d printing.

Lightning Talks

Patterns to build a scalable API – Amit Mishra

Genji – Embedded SQL database – Asdine El hrychy

AWS – Sean Tracey

Scratching your own itch – Christopher Stingl

Understand compiler errors – Varun Gandhi

Day 3

Taking the (Quantum) Leap with Go! – Mathilde Raynal