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.

Golang Diaries: Generics

147 pointsby ciprian_craciunabout 3 years ago

9 comments

cube2222about 3 years ago
Frankly, having used Go for years, I started using generics right after they were released and I&#x27;m very happy with them so far.<p>Sure, the performance characteristics could be improved, but other than that they solve all my main pain points I wanted them to solve while being constrained enough to not result in ivory towers on every corner.<p>My main pain points having been duplicated functions for each type as well as data structures.
评论 #31388557 未加载
评论 #31388484 未加载
评论 #31388466 未加载
morelispabout 3 years ago
Two thoughts:<p>1) The failure of<p><pre><code> func (t *fishTank) fishCount() string { return fmt.Sprintf(&quot;How many fishies? %d!&quot;, t.size()) } </code></pre> to work has nothing to do with generics; methods are always &quot;removed&quot; from types defined this way. If you want you can cast `t` to a `*container[fish]` and call it, but this is also where I would ask why &quot;extend&quot; rather than compose - are you gaining anything by ensuring identical layouts?<p>2) The bigger issue, that interfaces cannot require their implementations to be comparable, is <a href="https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;issues&#x2F;52614" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;issues&#x2F;52614</a> and linked tickets.
评论 #31388305 未加载
评论 #31388354 未加载
评论 #31387808 未加载
swidabout 3 years ago
Here&#x27;s the way you can solve the first problem with generics in go, almost exactly as he described:<p><a href="https:&#x2F;&#x2F;go.dev&#x2F;play&#x2F;p&#x2F;GrLYBj3N0jr" rel="nofollow">https:&#x2F;&#x2F;go.dev&#x2F;play&#x2F;p&#x2F;GrLYBj3N0jr</a><p>The trick is to define the fish tank like so:<p><pre><code> type fishTank struct { container[fish] }</code></pre>
评论 #31388682 未加载
gamblerabout 3 years ago
When I started using Go generics, they turned out to be completely useless for the use case I wanted to solve the most: functions to use in Go templates. Something is basic as <i>adding two numbers</i> is not solved by the core library and becomes a challenge with about a dozen numeric types. You can use a type switch and cast interface{}&#x2F;any to float64 (which is a hack, but one that works in practice). What you can&#x27;t do is use generics, because they require instantiation at compile time, which cannot happen in templates, because those are dynamic. Extremely disappointing.<p>Another disappointing thing is the fact that Go type inference is not good enough to have syntactically compact lambda functions. So now, even though it&#x27;s possible to write code that does filter-map-reduce kind of stuff, it&#x27;s verbose, slow to write and hard to read.
cpurdyabout 3 years ago
Adding a type system to an existing language that was never intended to have that type system is not simple. And in Go, the language seems designed to largely eject its type system at the earliest opportunity that doesn&#x27;t negatively impact the compiler. This will naturally constrain the capabilities of the generic programming constructs, much as it did in Josh Bloch&#x27;s Java generics implementation.<p>Go is going to be torn between the minimalism of its creators, and the desires for modern capabilities expressed by its vocal user base. If this were a movie, it would be &quot;A Beautiful Mind&quot;.
评论 #31388943 未加载
评论 #31393311 未加载
CraigJPerryabout 3 years ago
Not a Go developer but i&#x27;ve been playing with Go for a toy project, really just to learn it. I had a tiny play with generics in Go when i needed a &quot;min(int, ...int) int&quot; func<p><pre><code> &#x2F;&#x2F; --- There&#x27;s no int min() in stdlib ----------------------------------------- &#x2F;&#x2F; Option 1. use math.Min() which is defined for float64, benchmarks comparing this with Option 2 &amp; 3: &#x2F;&#x2F; BenchmarkMinImplementations&#x2F;math.Min-8 261596238 4.282 ns&#x2F;op &#x2F;&#x2F; BenchmarkMinImplementations&#x2F;minGeneric-8 588252955 2.037 ns&#x2F;op &#x2F;&#x2F; BenchmarkMinImplementations&#x2F;minVariadic-8 413756245 2.827 ns&#x2F;op &#x2F;&#x2F; Option 2. Generics in Go 1.18, this is generic over int and float but can&#x27;t support a variadic &quot;b&quot; func minGeneric[T constraints.Ordered](a, b T) T { if a &lt; b { return a } return b } &#x2F;&#x2F; Option 3. I was aiming for a lisp-style (min 1 2.3 -4) and this is variadic but it can&#x27;t also be generic in 1.18 func minIntVariadic(a int, bs ...int) int { for _, b := range bs { if b &lt; a { a = b } } return a } </code></pre> Coming from Java there&#x27;s less to get your head around, e.g. super vs. extends isn&#x27;t a thing here but otherwise similar.
评论 #31389387 未加载
yazaddaruvalaabout 3 years ago
Hey Tim, a long time ago I came to your office in Vancouver to talk about Ruler. Quamina seems very similar.<p>Are you learning Go with a familiar project? or is there another motivation for Quamina?
评论 #31401731 未加载
deeptoteabout 3 years ago
Go is my main language but it keeps disappointing me in it&#x27;s lack of implementation tuning detail. Generics are just a code generator that, given it&#x27;s poor performance for things like functional programming, makes it surprisingly of little use.<p>Go is a kitchen full of dull knives. It is good for layers of communication where you call other microservices to perform the heavy lifting, but not much else.
评论 #31387959 未加载
评论 #31389936 未加载
评论 #31388162 未加载
svnpennabout 3 years ago
&gt; func (c *container&gt;[S]) size() int {<p>This is not valid code.
评论 #31388221 未加载
评论 #31387632 未加载