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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Interface Upgrades in Go

174 点作者 zenazn超过 10 年前

8 条评论

dcposch超过 10 年前
The zero-copy IO in Go is so elegant.<p>I think you can really judge a language accurately by checking out its standard library. This is one of my favorite things about Go.<p>By comparison,<p>* The C++ STL. Fast and useful, but the implementation is nearly unreadable due to template soup. Here&#x27;s one of the simpler parts! <a href="https://www.sgi.com/tech/stl/stl_vector.h" rel="nofollow">https:&#x2F;&#x2F;www.sgi.com&#x2F;tech&#x2F;stl&#x2F;stl_vector.h</a><p>* PHP. So bad it&#x27;s basically a strawman. I&#x27;ll include it because it&#x27;s hilarious: <a href="http://www.forbes.com/sites/quora/2012/08/31/what-are-the-most-surprisingly-useful-php-functions/" rel="nofollow">http:&#x2F;&#x2F;www.forbes.com&#x2F;sites&#x2F;quora&#x2F;2012&#x2F;08&#x2F;31&#x2F;what-are-the-mo...</a><p>* Java. A bit better. Compare the readability of OpenJDK&#x27;s ArrayList.java to the STL vector.h, which does essentially the same thing: <a href="http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/00cd9dc3c2b5/src/share/classes/java/util/ArrayList.java" rel="nofollow">http:&#x2F;&#x2F;hg.openjdk.java.net&#x2F;jdk7&#x2F;jdk7&#x2F;jdk&#x2F;file&#x2F;00cd9dc3c2b5&#x2F;s...</a><p>But of course the Java standard library is immense and has a lot of cruft in it. To write clean Java you really need to avoid much of the standard library (read Effective Java by Josh Bloch) and add a few important missing parts (use Guava).<p>Golang is really unique in that regard. You can <i>learn Go</i> by reading the standard library. It is beautiful, linear code. The library is pretty complete--eg you can write a one-line HTTP file server out of the box--but nothing feels extraneous. Lastly, I think it gets close to Knuth&#x27;s ideal of Literate Programming. Paragraph-length comments thoughout the standard library explain what&#x27;s happening, how, and why.<p>For example, the post talks about how io.Copy is awesome. For a concise English explanation, why not go directly to the source!<p><a href="https://golang.org/src/pkg/io/io.go#L329" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;src&#x2F;pkg&#x2F;io&#x2F;io.go#L329</a>
评论 #8714567 未加载
评论 #8714583 未加载
评论 #8716832 未加载
评论 #8715547 未加载
andolanra超过 10 年前
The important thing is to <i>make sure that your &#x27;upgrade&#x27; is semantically indistinguishable from the non-upgraded version</i>, and to the extent that it differs, <i>document it</i>. There&#x27;s an old blog post[^1] where someone describes code that performed this kind of &quot;interface upgrade&quot; in a way that broke underlying assumptions about the code. This is a horrible (and unexpected!) kind of bug. One should be really, <i>really</i> sure that this is the correct way of solving a problem before actually reaching for this tool.<p>[^1]: In particular, a function which took a Reader would attempt to cast that Reader to something with a Close method, which broke the programmer&#x27;s assumptions about that function. It didn&#x27;t help that said behavior was at the time undocumentated: <a href="http://how-bazaar.blogspot.co.nz/2013/07/stunned-by-go.html" rel="nofollow">http:&#x2F;&#x2F;how-bazaar.blogspot.co.nz&#x2F;2013&#x2F;07&#x2F;stunned-by-go.html</a> and the HN discussion <a href="https://news.ycombinator.com/item?id=6060351" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=6060351</a>
TeeWEE超过 10 年前
Note that while interface upgrades are very usefull to use, they are not something very new in the programming language world.<p>In java you could do:<p><pre><code> if (reader instanceOf BufferedReader) { &#x2F;&#x2F; upgrade BufferedReader bReader = (BufferedReader) reader; } </code></pre> This is actualy adviced NOT to do, since its really a runtime type check.<p>In Go its no different, as this article explains. However its still a usefull mechanism sometimes.
skybrian超过 10 年前
I&#x27;m wondering what programmers in functional programmers do about this sort of thing? It seems like they have much stronger type constrants; subclassing and upcasting just plain don&#x27;t work. And if they did work, it would invalidate the sort of proofs that functional programmers like to do.
评论 #8715029 未加载
评论 #8716911 未加载
taliesinb超过 10 年前
Has anyone quantified the runtime cost of doing interface upgrades? I have a vague memory that the vtable for a specific type&#x2F;interface pair is constructed at runtime and then cached -- is this true?
评论 #8714552 未加载
scottpiper超过 10 年前
The author of that article is also the author of the Goji framework (very nice web framework for Golang): <a href="https://goji.io/" rel="nofollow">https:&#x2F;&#x2F;goji.io&#x2F;</a>
评论 #8715583 未加载
Goopplesoft超过 10 年前
Go&#x27;s interfaces are great, but one of my annoyances is that you can&#x27;t define an interface with values (methods only). Which makes little sense considering it works just fine with getters and setters. i.e:<p><pre><code> type Blah interface { Var string } </code></pre> wont work, but<p><pre><code> type Blah interface { GetVar() string } </code></pre> does. I&#x27;m sure theres a reason for this. Can someone shed some light?
评论 #8714611 未加载
评论 #8714589 未加载
评论 #8714579 未加载
shinamee超过 10 年前
Is it me or the text is really hard to read?
评论 #8714497 未加载
评论 #8714883 未加载
评论 #8714765 未加载