Build REST APIs from structs using Golang Generics


Golang 1.18 has been out for a while now. Are you using Generics?

At first, the author was delighted and started gradually switching from map and filter-like statements to github.com/samber/lo, and he created an internal slice package to add some more variants.

However, somewhere in his heart, he felt frustration building up. I felt like I was missing out on something like I didn’t understand its true value because I hadn’t used it to its fullest. It is as if I have been given such an interesting toy and have not played with it to the fullest.

I want to write Golang code using Generics!

I’ve been writing REST APIs in my daily job, and I’ve been thinking … isn’t this is the place in the conclusion of When To Use Golang Generics:

If you find yourself writing the exact same code multiple times, where the only difference between the copies is that the code uses different types, Consider whether you can use a type parameter.

Aren’t we writing the same code many times?
Aren’t we writing REST APIs over and over again?
Isn’t it time to use Golang Generics to build REST APIs?

I thought I’d give it a shot, and have experimented with Golang Generics for a few days. The results are as follows.