Disclaimer: I've only played with Go for one day now, so there's a good chance I've missed a lot.
Does anybody know why there is no real support for generics/templates/whatsInAName in Go? So there is a generic map
, but that's supplied by the compiler, while a Go programmer can't write her own implementation. With all the talk about making Go as orthogonal as possible, why can I USE a generic type but not CREATE a new one?
Especially when it comes to functional programming, there are lambdas, even closures, but with a static type system lacking generics, how do I write, well, generic higher order functions like filter(predicate, list)
? OK, Linked lists and the like can be done with interface{}
sacrificing type safety.
As a quick search on SO / Google did not reveal any insights, it looks like generics, if at all, will be added to Go as an afterthought. I do trust Thompson to do way better than the Java guys, but why keep generics out? Or are they planned and just not implemented yet?
this answer you will find here: http://golang.org/doc/faq#generics
Why does Go not have generic types?
Generics may well be added at some point. We don't feel an urgency for them, although we understand some programmers do.
Generics are convenient but they come at a cost in complexity in the type system and run-time. We haven't yet found a design that gives value proportionate to the complexity, although we continue to think about it. Meanwhile, Go's built-in maps and slices, plus the ability to use the empty interface to construct containers (with explicit unboxing) mean in many cases it is possible to write code that does what generics would enable, if less smoothly.
This remains an open issue.
Go 2
There is a draft design for generics at https://blog.golang.org/go2draft.
Go 1
Russ Cox, one of the Go veterans wrote a blog post entitled The Generic Dilemma, in which he asks
…do you want slow programmers, slow compilers and bloated binaries, or slow execution times?
Slow programmers being the result of no generics, slow compilers are caused by C++ like generics and slow execution times stem from the boxing-unboxing approach that Java uses.
The fourth possibility not mentioned in the blog is going the C# route. Generating the specialized code like in C++, but at runtime when it is needed. I really like it, but Go is very unlike C# so this is probably not applicable at all…
I should mention that using the popular Java 1.4 like technique of generic programming in go that casts to interface{}
suffers from exactly the same problems as boxing-unboxing (because that's what we are doing), besides the loss of compile time type safety. For small types (like ints) Go optimizes the interface{}
type so that a list of ints that were cast to interface{} occupies a contiguous area of memory and takes only twice as much space as normal ints. There is still the overhead of runtime checks while casting from interface{}
, though. Reference.
All projects that add generic support to go (there is several of them and all are interesting) uniformly go the C++ route of compile time code generation.
Even though generics are not currently built-in, there are several external implementations of generics for go, that uses comments in combinations with small utilities that generate code.
Here is one such implementation: http://clipperhouse.github.io/gen/