TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A Proposal for Adding Generics to Go

225 pointsby jpariseover 4 years ago

26 comments

oefrhaover 4 years ago
Related earlier discussions:<p>- <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20576845" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20576845</a> (2019 draft)<p>- <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20541079" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20541079</a> (also 2019 draft)<p>- <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=23543131" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=23543131</a> (2020 draft, i.e. the base version of the current draft)
评论 #25754503 未加载
tschellenbachover 4 years ago
We have a very large Go codebase here at Stream and not having generics is just not really as big of an issue as you think it is. There are plenty of work arounds if you get used to not having generics in the language. The fast compile times of Go are amazing. I was doing some Kotlin a few weeks ago and the difference is crazy. Go: Install deps, compile everything done in 5s. Doing the same in Kotlin, laptop freezes, android studio freeze, time to get a coffee :)<p>That being said it would be really nice to have some reusable map type structures that handle GC better than the default maps. Fingers crossed.
评论 #25752130 未加载
评论 #25751046 未加载
评论 #25753536 未加载
评论 #25753671 未加载
评论 #25751130 未加载
评论 #25752105 未加载
评论 #25751810 未加载
评论 #25751146 未加载
评论 #25760742 未加载
评论 #25751204 未加载
评论 #25751115 未加载
评论 #25752450 未加载
评论 #25752971 未加载
评论 #25754049 未加载
Animatsover 4 years ago
Two more levels of blogs down, the actual proposal.[1] Definition:<p><pre><code> &#x2F;&#x2F; Print has a type parameter T and has a single (non-type) &#x2F;&#x2F; parameter s which is a slice of that type parameter. func Print[T any](s []T) { ... } </code></pre> Call:<p><pre><code> Print[int]([]int{1, 2, 3}) </code></pre> Above, &quot;any&quot; is really just a synonym for &quot;interface{}&quot;. You can have more restrictive type constraints on parameterized types by specifying other Go interfaces. This is vaguely similar to how Rust does it, and quite different from the C++ approach.<p><i>&quot;This design does not support template metaprogramming or any other form of compile time programming.&quot;</i><p>[1] <a href="https:&#x2F;&#x2F;go.googlesource.com&#x2F;proposal&#x2F;+&#x2F;refs&#x2F;heads&#x2F;master&#x2F;design&#x2F;go2draft-type-parameters.md" rel="nofollow">https:&#x2F;&#x2F;go.googlesource.com&#x2F;proposal&#x2F;+&#x2F;refs&#x2F;heads&#x2F;master&#x2F;des...</a>
评论 #25751723 未加载
评论 #25751724 未加载
rowanseymourover 4 years ago
I rarely find myself frustrated with the lack of generics in Go and am so glad to never deal with the kind of over-engineered generic madness that is so common in Java, except...<p>When dealing with collections. It&#x27;s maddening to have to keep duplicating basic functions like getting the keys from a map, or checking if a slice contains a given item.
评论 #25754163 未加载
评论 #25753332 未加载
评论 #25753631 未加载
anderspitmanover 4 years ago
I&#x27;ve been heavy into Go the past year. I love the simple interfaces they&#x27;ve built over some rather complicated things (concurrency, cross-compilation, networking, etc), which really do tend to just work.<p>I fear that Go will eventually turn into something where we look back and realize we&#x27;ve lost something important by gaining a lot of less importants. The impulse to <i>change things</i> is just too strong these days. C89 has done just fine unchanged for 30 years.<p>All I want is a C+=1 I can rely on for the next 30 years.
评论 #25752013 未加载
评论 #25752930 未加载
评论 #25811727 未加载
jagger27over 4 years ago
Here&#x27;s the actual proposal:<p><a href="https:&#x2F;&#x2F;go.googlesource.com&#x2F;proposal&#x2F;+&#x2F;refs&#x2F;heads&#x2F;master&#x2F;design&#x2F;go2draft-type-parameters.md" rel="nofollow">https:&#x2F;&#x2F;go.googlesource.com&#x2F;proposal&#x2F;+&#x2F;refs&#x2F;heads&#x2F;master&#x2F;des...</a>
kitkat_newover 4 years ago
I hope at some point they manage to add it.<p>I, however, discovered Rust in the meanwhile. It has generics. And it is not too complex either and has quite a few other bonuses.
评论 #25751898 未加载
评论 #25751023 未加载
评论 #25751082 未加载
评论 #25751199 未加载
评论 #25758048 未加载
cratermoonover 4 years ago
I look forward to generics in Go. Yes, it&#x27;s possible to do it with reflection, interfaces and interface{}, but it&#x27;s not typesafe, it&#x27;s not fast, and it&#x27;s prone to code bloat.<p>I&#x27;m a fairly late-comer to generics, I never programmed seriously in C++, I avoided generics in Java initially, and I wrote a lot of code in less statically-typed languages. Ever since the first serious talk of generics in Go 2.0 I&#x27;ve endeavored to educate myself and I now am very strongly in favor of them.
评论 #25752464 未加载
ed25519FUUUover 4 years ago
I&#x27;m really mixed about this. As a developer I would <i>love</i> having generics in Go. I can think of a few places in my code I can greatly simplify if they were implemented now.<p>However, as someone who reads <i>other people&#x27;s</i> Go code, I&#x27;m not a huge fan. One of the greatest things about Go is that a developer can usually one-shot read and understand almost anybody&#x27;s code because there&#x27;s a simplicity &quot;forcing-function&quot; applied to everything. To lose that would be a shame.
评论 #25752010 未加载
评论 #25751900 未加载
candiddevmikeover 4 years ago
It&#x27;s too bad this is targeting end of year, I have so many applications for this--test assertions, http controllers, SQL--this will remove a lot of duplicate code. I also think it will expand use cases for Go, especially in the UX area where you have to implement duplicative getters and setters.
cletusover 4 years ago
Another comment mentions this is the third (serious) attempt at adding generics to Go.<p>Is there any concise history of these attempts (including this one)? I&#x27;d like to understand the gist of these proposals and what ultimately derailed them.<p>By themselves these proposals are pretty inscurtable.
评论 #25752761 未加载
评论 #25751085 未加载
azhenleyover 4 years ago
How many different proposals for generics have there been?
评论 #25751189 未加载
评论 #25750883 未加载
评论 #25751242 未加载
评论 #25750895 未加载
评论 #25750920 未加载
评论 #25750946 未加载
dgb23over 4 years ago
I find the proposal interesting. Type constraints might help to reason about a given abstraction. If I understand correctly they behave quasi like sum types over interfaces.<p>From skimming here: <a href="https:&#x2F;&#x2F;go.googlesource.com&#x2F;proposal&#x2F;+&#x2F;master&#x2F;design&#x2F;go2draft-type-parameters.md" rel="nofollow">https:&#x2F;&#x2F;go.googlesource.com&#x2F;proposal&#x2F;+&#x2F;master&#x2F;design&#x2F;go2draf...</a><p>They don&#x27;t feel like generics in for example Java (my Java is rudimentary), but rather like an abstraction over interfaces.<p>Can anyone elaborate on this?
hackyhackyover 4 years ago
As much as I&#x27;ve cursed the lack of generics and the limited expressiveness of Go&#x27;s type system, it&#x27;s hard for me to reconcile these proposals with what I know of Go. Go was conceived as a small language, a successor to C, and purposely eschewed &quot;new-fangled&quot; features of modern languages. Whether the result is good is a matter of taste, but I feel that retroactively bolting on a modern type system will simultaneously (a) undercut the simplicity of the core language and (b) produce a language that is not as clean as those conceived with generics from the beginning.
评论 #25751053 未加载
评论 #25752736 未加载
评论 #25751119 未加载
评论 #25751054 未加载
评论 #25752066 未加载
msieover 4 years ago
&quot;If the proposal is accepted, our goal will be to have a complete, though perhaps not fully optimized, implementation for people to try by the end of the year, perhaps as part of the Go 1.18 betas.&quot;
systemvoltageover 4 years ago
Side tracking a bit: I wish there was a popular programming language like Go with rust-like package manager, Python style syntax and ability to hack, compilable, classic (classes, methods), and fast. Or I wish Go had classic OOP and raise Exception methods.<p>Basically, I want fast statically typed python with better package management. Or other way to put it, I want Go with classic OOP and Exceptions.
评论 #25752103 未加载
评论 #25752837 未加载
wrnrover 4 years ago
Sometimes I miss java stream API in Go, maybe this can be a first step to implementing a similar lazy functional programming interface.
majewskyover 4 years ago
I&#x27;m incredibly interested† to see how this is going to affect kubernetes&#x2F;client-go and friends.<p>† Deliberate use of a neutral adjective.
_QCanaria_over 4 years ago
The carefulness of the Go team when introducing new features is remarkable.<p>After many years chasing the newest, shiniest and best tools I can&#x27;t appreciate stability and a large and useful standard lib enough. I finally understood the importance of the boring stack.<p>I don&#x27;t need generics in Go, but I&#x27;m happy they are coming. Especially for collection methods.
alkonautover 4 years ago
Finally. Hope it gets approved.<p>It&#x27;s strange that they don&#x27;t consider Print[T](x T) instead of Print[T any](t T). The &quot;any&quot; could just be omitted without loss of anything. Especially since repeated types with the same constraints indeed DO omit it! Print2[T1, T2 any]
评论 #25755334 未加载
评论 #25753592 未加载
djhaskin987over 4 years ago
Would someone mind enlightening me as to what the difference is between this draft and the 2020 draft.
评论 #25754527 未加载
betimslover 4 years ago
&gt; Interface types used as type constraints can have a list of predeclared types; only type arguments that match one of those types satisfy the constraint.<p>Wait... But why?gif
the_arunover 4 years ago
Slowly Go will become Java :)<p>Go -&gt; Guava -&gt; Java
jcelerierover 4 years ago
every language designer who does not understand c++ is doomed to reinvent it<p>I mean, seriously,<p><pre><code> func F[T any](p T) { ... }</code></pre>
评论 #25751043 未加载
评论 #25752796 未加载
eplanitover 4 years ago
I wonder how long it will take for Go to become like Java by adding baggage like this.<p>As soon as you see Enterprise Go, it&#x27;s over.
smokey_circlesover 4 years ago
Generics are awful. They solve no problem in the domain space, only the developer space.<p>Which then creates the problem of developers who insist on writing infinitely extensible generics with indecipherable bounds.<p>Just repeat code. You&#x27;ll be fine.<p>If you find yourself repeating a LOT of code because Go does not support generics, maybe stop and think about your design. Putting generics in as an escape hatch will do you more than good, guaranteed