HACKER Q&A
📣 philosopher1234

Was it worth it for Go to add generics


Adding Generics seemed like a massive effort, but I don’t see any major changes to existing or new Go code since their release. Maybe they weren’t so important?


  👤 Tea418 Accepted Answer ✓
The public Go code I usually read are libraries and I admit I also noticed only a little adoption of generics.

My personal hypothesis is that many libraries follow the official Go release policy[1]. Due to the fact that generics were introduced with v1.18 and we now have v1.19, libraries would be bound to the features that were available with v1.17, unless they spend effort on working around this by implementing the respective build flags that make generics available exclusively for >=v1.18. However this is just an assumption.

According to the Go Developer Survey 2022 Q2[2], 1 in 4 respondents said they've already started using generics in their Go code and 14% have started using generics in production code.

[1] https://go.dev/doc/devel/release [2] https://go.dev/blog/survey2022-q2-results


👤 Jtsummers
It's only been available for 6 months, the language has been around for nearly 13 years (November 2009). Pretty much all learning materials that aren't intended to teach generics (which have been written only in the past few months, or the past couple years about prospective generic mechanisms) will not teach generics. Anyone picking up The Go Programming Language or any other text or watching any video course will not be taught to use generics. And since there has been about 12.5 years worth of code written without generics and that code works, there's little need to massively change anything.

So until the learning material catches up and until new libraries are written, or old ones have a reason to be updated, you won't see generics widely used.


👤 basicallybones
Generics in Go are very helpful for my team. They allow us to create reusable functions in common libraries in our monorepo that are simple to reason about and easy to read.

For instance, we have an internal library that helps us create and perform common operations on concurrency-safe maps. Being able to pass and return arbitrary structs to/from those functions has made our codebase much cleaner and easier to work with.

Please take my response with a grain of salt, as we have not benchmarked or otherwise compared (via escape analysis, etc.) those generic functions against any non-generic equivalents.


👤 faraaz98
If people aren't making too much noise about a language feature then I just assume it was implemented right

👤 dangoor
Yes! I spent some time a few weeks ago redoing some code that relied heavily on reflection into code that is fully statically typed and this only works because of generics. The particulars is that I have a YAML file that defines some validation information for incoming data and the old code used to dynamically compare the expectations from the YAML file with the data. The new code statically defines everything that's allowed in the YAML file and turns that into a small number of function calls to validate the incoming data.

It's a very frameworky piece of code, which is exactly the kind of thing that can benefit from generics.


👤 PeterCorless
ScyllaDB used Go generics to get a 40% gain for B-Tree performance

https://www.scylladb.com/2022/04/27/shaving-40-off-googles-b...


👤 anxiously
They were not necessary but useful in some contexts. What you are noticing is people not overusing them (which was a major concern).

The only time I have used them in the last year was building a generic LRU memory cache and it worked wonderfully.


👤 davewritescode
It takes a while for ecosystems to catch up. Java 8 primitives in library code took a few years to happen, I assume the same thing will happen in Go.

Generics are typically very useful in a few very specific contexts but in most applications you can get by fine without them.


👤 gregwebs
Unfortunately we have to wait. Generics handle simple cases well, but have issues with more complex edge cases. Every go release is improving Generics. 1.19 has a performance regression that will be fixed in 1.20.

Library authors want to support old go versions, and the library usage of Generics might not work on that older version.

I just started using generics because I wanted a couple release cycles for issues to be shaken out. I would suggest to most library authors to wait until 1.20 is released before using generics, and if you are doing anything slightly sophisticated to make sure you have a CI that builds with older versions of Go.

If you are writing application code, then go ahead and use generics, but generics are usually most useful for libraries.

I was recently able to fork a library for better error handling in Go and make a version similar to the try proposal. This library wouldn't have been possible without generics: https://github.com/gregwebs/try


👤 bugfix-66
Generics are useful for some purposes. But mostly they can be avoided in favor of simpler language constructs. Most Go developers understand the importance of using simple code constructs.

There are many experienced developers who (through long experience fighting C++) have come to understand that using a simple language (like Go or C) allows you focus on the algorithms and complexity of the problem you're trying to solve. This is part of the Go culture.

A junior developer might not understand this yet. He's still having fun finding obscure and convoluted contortions of the language that allow him to save little or no effort... not yet realizing that nobody gives a shit and he's wasting his time and the time of his coworkers.


👤 jeffrallen
I'm an avid Go user with zero interest in generics. I figured I'd learn then when I had to, i.e. when a dependency breaks and I need to go debug it and it uses generics.

I still haven't learned generics.


👤 nickcw
I looked through the rclone code to see if I could use generics.

In particular I looked at places where we use code generation and places where we use reflection - both good candidates for generics.

Alas I found generics just weren't powerful enough to replace these uses, it would need a more powerful macro system.

I think go generics are great for container libraries. I've written and debugged enough of those to be very happy using a library.

I doubt Go will ever get macros though.

I really like Zig's comptime mechanism - that seems like the right amount of power and it's all just zig code!


👤 throwaway894345
I have pretty mixed feelings about them. My opinion hasn't changed dramatically since they've been released--they make certain things easier, and they lead to people overcomplicating things. I also don't care for the dict-based implementation--it makes it considerably harder to reason about performance tradeoffs.

Mostly though, most of the stuff people were complaining about regarding Go's lack of generics were really minor things, like map/filter versus for loops. There are some things you really couldn't do easily in Go without generics, but for the most part Go's critics were just obstinate about not wanting to learn a new style of programming (so much so that many are willing to forsake all of Go's considerable advantages with respect to tooling, deployment model, build times, package management, ecosystem, etc). Moreover, Go's critics were (and still are) unwilling to admit there are benefits to avoiding generics--the code becomes less abstract and more standard as there are fewer ways to express a given solution.

If you really want to dunk on Go, the more valid criticism is that it lacks sum types, and unlike converting a map/filter chain to a for loop, there is no good way to emulate sum types in Go (there are some tricks to get close, but they involve far more boilerplate and they don't give exhaustiveness checks (and the performance tradeoffs suck).


👤 mattnewport
People who care about generics already decided not to use Go because it didn't suit them. People who used Go value different things in a language or they wouldn't have been using Go.

👤 lm28469
I've seen them used in places we could have skipped them more often than in places we needed them.

👤 Kukumber
that's a success if it didn't cause any harm

Go is the cloud native language, there is no need to change your production code only just to add the latest shiny feature, that goes against the philosophy of Go

people will adopt the new features slowly but surely as they get battle tested


👤 wara23arish
I wrecked my brain last week trying to get generics to work correctly and i think Go just won’t support this.

have a function take in an array of T

T is generic with type constraint call Constraint

problem is calling that function wont work because it has to instantiated with one of the types found in the Constraint which beats the whole purpose. I need this to pass in structs that belong to Constraint.

I tried a multitude of different things and nothing seemed to work.

I ended up reading the Go library for how it handles SPrintF args and did something similar with a type switch and reflection wizardry.

Im still a junior programmer so it could be that I just didnt know how or I underestimate the solution for this.

But I figured once we have a type Constraint defined with a function, it should be relatively simple to not have to instantiate it with ONE concrete type and allow the Constraint types as args.

if anyone knows a better way to do this please let me know


👤 michele
I have been using go for maybe 6-7 years and couldn't care less about generics. However, I now have to admit I'm quite happy they added them and I used them a bunch of times already to clean up my code and make re-usable types/functions across our stack.

So was not having generics a deal-breaker? No! Are they useful? Yes!


👤 nvartolomei
I tried bringing them to a few relatively small codebases and the most frequent feedback is “why introduce this complexity when you can write a for loop in place”. Must be some sort of Stockholm syndrome that affects people “writing go for 13 years”.


👤 verdverm
Is it too early to answer this question?

👤 rgoulter
There used to be a troll repo on Facebook's GitHub org about Go generics, which is now archived, so ... maybe?

https://github.com/facebookarchive/generics