4 ms·
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
by basicallybones 4y ago
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.
- dmitryminkovsky 4y agoI would have to imagine that at runtime there is no difference between a generic and non-generic function. I imagine (emphasis on imagine: no actually knowledge) that generics are used at compile time to check types? I have no idea whether Go erases generics though. Maybe introspection is slower with them?
- gmfawcett 4y agoWorth reading: https://go.dev/blog/when-generics https://go.dev/blog/when-generics
- throwaway894345 4y agoGo's generic implementation (sadly) tries to reduce the amount of code it generates by generating code which dispatches to the concrete methods at runtime. The rules for when this dispatch happens aren't easy to understand, and they're subject to change, so reasoning about performance (already hard) gets strictly harder. In some cases, generics might make code faster, and in others it may make code slower.
- throwawaymaths 4y agoThere is a difference. Most people (who use go for network IO bound services) will not notice.