TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Generics enabled by default in Go tip

264 点作者 dcu超过 3 年前

25 条评论

_ph_超过 3 年前
I like Go very much, a huge role plays its simplicity. In my career, I have been much more often bitten by having to deal with the complexity of a language then by not being able to do things, because a language feature is missing. That is, why I like Go so much. It strikes a great balance between important high-level features (GC, first class functions and closures) and still being a simple language (like Scheme is, in a sense they are very comparable in my eyes). The interface concept is great for covering a lot of cases. That is, why so far, I have not missed generics much and was rather reluctant to the constant wishes to add them to the language.<p>On the other side, there are cases, even if they are not very frequent, where generics do help a lot. Just the new slices package allone almost justifies the addition of generics. Furthermore, the proposal looks very much Go-like. As a consequence, I am quite looking forward to try them out and used responsibly[1], they can be a great addition to the Go universe.<p>1: I think the situation is somewhat similar to Lisp macros. Normally, a codebase rarely requires macros and readability is better, if you avoid creating too many macros. But occasionally there are situations, where using a macro tremendeously increases code quality. Both implementation and readability-wise. There macros should be used and it is great to have them available.
评论 #28256942 未加载
评论 #28256922 未加载
peter_l_downs超过 3 年前
Super excited for this. If you haven&#x27;t checked out the latest proposal, here&#x27;s an example of what Option&#x2F;Result box types might look like (obviously not ready for release, just an experiment):<p><a href="https:&#x2F;&#x2F;go2goplay.golang.org&#x2F;p&#x2F;krvTH1_7lwX" rel="nofollow">https:&#x2F;&#x2F;go2goplay.golang.org&#x2F;p&#x2F;krvTH1_7lwX</a>
评论 #28254141 未加载
评论 #28254081 未加载
评论 #28254507 未加载
评论 #28255338 未加载
评论 #28269988 未加载
评论 #28254099 未加载
评论 #28254659 未加载
evanmoran超过 3 年前
This is actually my first week using go (from years of C++&#x2F;Swift&#x2F;js&#x2F;etc) and I’ve been very impressed with the module system and simplicity so far. I’d definitely encourage others to try it if they haven’t.<p>As for generics, Go’s lack of function overloading and arg default values is really interesting. It ensures that a function is always easily found as it’s the only thing in the module with that name. I’ll be curious to see if generics are easier to follow without function overloading. It will still be only in one place, where as in C++ you could have hundreds of functions with the same name across many libraries and you just have to hope your ide knows which to step into.
评论 #28254751 未加载
Taek超过 3 年前
Not excited about this feature, I guess we&#x27;ll see how frequently it shows up in unwanted places. Generics in C++ really damage the readability of the code sometimes, maybe the go devs have found a better way.<p>Very skeptical, but the go devs have given me plenty of pleasant surprises before, maybe we get another one here.
评论 #28254441 未加载
评论 #28254202 未加载
评论 #28254207 未加载
sascha_sl超过 3 年前
If you&#x27;re really concerned about &quot;clever developers&quot;, maybe you ought to ask for the removal of reflect and ast, because they&#x27;ve both been used to be way too clever about solving the same issue.
评论 #28258234 未加载
amw-zero超过 3 年前
Generics introduce complexity. That’s pretty undeniable. There are also cases where generics provide much better solutions to problems. That is also undeniable.<p>As someone who’s working in a Go codebase at work, I’m happy they added generics, especially in the way they did. It’s a pretty minimal subset of generic functionality. I will apply it in certain cases, mostly to reusable code. I think it’s a big win overall though, without introducing too much complexity to the language.
3np超过 3 年前
Wow, it&#x27;s happening! For others completely out of the loop on that there was even an accepted proposal that is now being implemented:<p><a href="https:&#x2F;&#x2F;go.dev&#x2F;blog&#x2F;generics-proposal" rel="nofollow">https:&#x2F;&#x2F;go.dev&#x2F;blog&#x2F;generics-proposal</a>
评论 #28254037 未加载
评论 #28254145 未加载
评论 #28256115 未加载
awinter-py超过 3 年前
those aren&#x27;t generics they&#x27;re from the canadian aboriginal syllabic block
评论 #28255036 未加载
smokey_circles超过 3 年前
Language flamewars on internet forums are... strange.<p>Why have we all so strongly coupled our identities as programmers to the language we use? Sense of community and a perceived need to defend it?<p>I don&#x27;t think Go needs generics, but I&#x27;m not about to invent obscure edge cases to justify for&#x2F;against the idea. That&#x27;s a recurring theme in all defenses of any language. It&#x27;s not helpful.<p>Use Go if you like the &quot;clarity&quot;, stay away from it if you don&#x27;t like the &quot;verbosity&quot;.<p>These are both valid points, but they&#x27;re subjective. Pushing it as fact is dishonest and self-serving, as well as eventually detrimental.
评论 #28256211 未加载
评论 #28259574 未加载
评论 #28258767 未加载
评论 #28259164 未加载
评论 #28259081 未加载
评论 #28257069 未加载
评论 #28255395 未加载
评论 #28265913 未加载
brundolf超过 3 年前
I&#x27;ve never used Go, and from the outside I take a lot of issues with its design choices.<p>But even without having used it I always thought the lack of generics was very interesting and I could see how it was desirable. It&#x27;s fascinating to me that Go has gotten as far as it has without them (proving that it&#x27;s possible to), and the mindset shift people describe having around them seems like a really important thing to pay attention to.<p>One thing I&#x27;m curious about: user-defined generics I can see going without, but the core language and standard library surely need to have them. How does that work in practice? Is there a syntax for using them and just not one for creating them? How are they defined in standard library code?
评论 #28254367 未加载
评论 #28254776 未加载
评论 #28256815 未加载
评论 #28254684 未加载
i_have_to_speak超过 3 年前
It is available in Go 1.17 with a flag, use:<p><pre><code> go run -gcflags=-G=3 foo.go</code></pre>
评论 #28254079 未加载
yawaramin超过 3 年前
I&#x27;m not a Go user; I don&#x27;t consider it serious as a programming language. True, it has great tooling. It seems perfectly designed for certain groups of devs to bang out lower-level tools and utilities, and they love it for being such a great fit.<p>That said, I think some of them may have a point that Go shouldn&#x27;t actually try to tack on generics now. Consider that generics are only one piece of the puzzle when it comes to programming language power and abstraction capability. There are also ADTs, abstract types, pattern matching, immutability, lack of null, expression-oriented syntax, etc.<p>Go with generics would be only one step along that journey of abstraction power. It would help the set of people who desperately need it to solve their code repetition and type-casting issues. But it would solve these problems just to clear the way for them to reach the next set of problems on the journey of abstraction.
tannhaeuser超过 3 年前
Not entirely getting the hate&#x2F;hype towards generics. What about run-time reflection and annotations, arguably an addition with more drastic consequences, and all things considered, not for the better IMO. Has anyone an opinion on using GraalVM&#x2F;Java vs Golang?
jagger27超过 3 年前
Pretext: I love Go and write most of my day job code in it.<p>To the people moaning about how generics will make their favourite language as awful and ugly as Java: all of the libraries and techniques you like and use today will always work. Little things like sorting will use generics pretty much transparently. All of the strongly typed code you write today that receives and returns concrete types will be just as valid tomorrow as it is today. Interfaces keep the same semantics and are a core part of how generics work.<p>I think it’s important to remember that the people who design this language like it for the same reasons you do.
评论 #28255823 未加载
pjmlp超过 3 年前
Finally catching up modern times!<p>Welcome to 1976.
tapirl超过 3 年前
Go needs custom generics for sure. But it is really a hurry-up to support custom generics for Go in version 1.18. Many problems have not been resolved yet.<p>* should the builtin generics syntax be compatible with the new custom syntax<p>* how many problems will be solved by the custom generics and how much complexities will be added. Is it a good balance?
评论 #28255377 未加载
silisili超过 3 年前
Not to be contrary for its sake, but I&#x27;ll say this is one change I&#x27;m really not happy about.<p>I feel like it&#x27;s a change to placate many, while driving a lesser amount away. Which is fine, but still feels like the end of something, as I am one of the aforementioned &#x27;lesser.&#x27;<p>As for why...I love above most the simplicity and readability of Go. Any change which encroaches that, which this does, is a net negative to me.
评论 #28254399 未加载
评论 #28254352 未加载
评论 #28254152 未加载
评论 #28254227 未加载
评论 #28254153 未加载
评论 #28254829 未加载
评论 #28254174 未加载
评论 #28254113 未加载
评论 #28254810 未加载
评论 #28254334 未加载
评论 #28254460 未加载
评论 #28254392 未加载
mgraczyk超过 3 年前
Glad to see this finally happening. I&#x27;ve been writing a decent amount of Go lately and there are plenty of instances where this will make the code more readable.
tedunangst超过 3 年前
Every HN thread about go: go is useless because it lacks generics.<p>Go adds generics.<p>HN thread: I don&#x27;t want this.<p>Good case study about the people drawn to comment on a topic.
评论 #28254654 未加载
评论 #28255673 未加载
评论 #28254339 未加载
评论 #28255034 未加载
评论 #28254603 未加载
评论 #28254230 未加载
评论 #28255350 未加载
评论 #28258766 未加载
评论 #28256251 未加载
评论 #28256890 未加载
评论 #28254485 未加载
评论 #28254820 未加载
ledneb超过 3 年前
What syntax did Go settle on, in the end?
评论 #28255723 未加载
ammmir超过 3 年前
the generics mafia has ruined rust and looks like go is next.
评论 #28256411 未加载
shp0ngle超过 3 年前
Hey, instead of “return value, err”, we can now write it as generic Option!<p>Kidding kidding, please don’t do it kids
throwawaygo超过 3 年前
Sadly go is losing the spirit it was built with and alienating the experience that it needs to regain that spirit. This has been my top feedback every go survey is that it is being designed democratically where it needs to be led by experience.
评论 #28254691 未加载
durbatuluk超过 3 年前
If you want all features of language X (I doubt we will stop at generics) use language X. Stop trying to make all languages the same.<p>I work with programmers from OO background (java) and they can&#x27;t even grasp the utility of functions as value or closures. Every damn &quot;service&quot; has an interface&#x2F;generated mock and anemic model. They&#x27;re desperate waiting for generics for go to &quot;complete&quot;.<p>I fear the influx of OO programmers.
评论 #28255435 未加载
valenterry超过 3 年前
Two thoughts:<p>1.) In a &quot;simple language&quot; (e.g. without generics) each line of code is easy to read&#x2F;understand. But reading&#x2F;understanding the whole program or application is difficult. In &quot;not-simple languages&quot; it&#x27;s the other way around.<p>2.) An important criteria to judge the future of a language is how foresight the language authors have. I read that the go authors said in the past that they were not ready to add generics because they didn&#x27;t know how to do so in a good way. True or not, retrospectively adding generics is not a great indicator for good language design to me. Even if the addition of generics is a net-positive thing, I expect that Go will go in the direction of C++, having a lot of accidental feature complexity in the language.<p>I think it&#x27;s better to do it like Lisp and keep a simple (but nonetheless flexible) core. Or do it like Haskell and design the language to elegantly allow as much abstraction as possible, moving carefully towards that goal. For these kind of languages, you better have people who have years long experience in exactly this: designing powerful programming languages. A developer can be the best in their own field, but they are doomed to fail when trying to build a future-proof, well designed programming language on their first attempt. (btw, not relating to the Go author&#x27;s here)<p>An example where this didn&#x27;t work is Angular - from the first moment that I got in touch with it, I knew it was built by amateurs. It&#x27;s only a framework, but the difference to a programming-language is minor in the case of these kind of all-encompassing frameworks. It is not surprising to me at all that the completely redesigned Angular later on.