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.

Interface Upgrades in Go (2014)

88 pointsby panic11 months ago

10 comments

nickcw11 months ago
I used to use interface upgrades in rclone (a program which syncs your data to many different cloud providers) for all the optional features a backend (cloud provider) might have, for example renaming a file.<p>However the proxy problem became unmanageable. That&#x27;s when I had a backend (eg the crypt backend which encrypts any other backend) which wraps another backend. Can the crypt backend rename things? That depends on what it is wrapping and you&#x27;d have to upgrade the interface call it and then get a special error to find out.<p>Eventually I switched to a table of bound method pointers which were easy to test against nil to see whether they were implemented and could be filled with a very small amount of reflection.<p>In my experience interface upgrades are useful but the proxy problem is very real so use sparingly only!<p>I did suggest at one point (to rsc) an addition to Go which would allow interface proxies to take methods away from their method set at run time to fix this problem, so the proxy could only have the methods it could implement. I&#x27;ve no idea how difficult this would be to implement though.
评论 #40768900 未加载
评论 #40767797 未加载
评论 #40767216 未加载
评论 #40768254 未加载
PUSH_AX11 months ago
I remember working in a team that used interfaces for a ton of things. Every single one of those things only ever had one concrete implementation, still to this day. A lot of those things were also purely for mocking in tests too.<p>Today I don’t use them, unless I need it retrospectively. Which I find is rare.<p>This is not a knock on interfaces as a language feature. Just inserting a random anecdote about pragmatism..
评论 #40771992 未加载
评论 #40765799 未加载
karmakaze11 months ago
This is basically a hack to get pragmatic performance at a cost of comprehensibility. It would have been better if Go allowed union types then a func could be declared to use interface A|B efficiently. Passing a narrow interface when a wider implemented one could get used is lying about the actual &quot;interface&quot; in the signature. Added to that is that Go&#x27;s interfaces are structural <i>(which I think are great)</i>, but could lead to accidental misuse by passing in interface A for an object that also has method X for unrelated purposes that co-incidentally satisfies interface B which the called func on A magically uses.<p>&gt; Like all articles about Go’s interfaces, we are obligated to start with Go’s io package.<p>Also the io package, or stdlib in general is not a good place to look for good patterns to use in Go. Numerous antipatterns are used in the name of performance. The principles for stdlib authors and recommendation for Go developers are different. As an example io functions can return a value AND an error--and whether to continue or not depends on the specific error <i>(as some are benign)</i>. <i>It&#x27;s better that I don&#x27;t name an example as you should always be on the lookout for (until having learned) them.</i>
评论 #40768067 未加载
rollulus11 months ago
Note that this is not necessarily a great thing to overuse. Also note that the article is 10 years old.<p>Relying on such upgrades sort of introduces a dark and fuzzy part of the API. Go’s http pkg is a notorious one, with how a http.ResponseWriter can also be a Flusher and Hijacker and I don’t know what else. If you in your middleware want to wrap it, you need to implement those interfaces as well, or the functionality is lost. But they’re not part of the “visible” API, they’re type assertions buried deep in the standard library, good luck with that. For this reason Go 1.20 introduced the http.ResponseController.
评论 #40767409 未加载
geoka911 months ago
&gt; While it just so happens that all the ResponseWriters that net&#x2F;http give you implement (e.g.) CloseNotifier, there’s not really any way for you to know that without reading the source.<p>Go editor tooling helps with this: gopls can do this nowadays (e.g. `lsp-find-implementation` in Emacs) and go oracle&#x2F;guru may have already supported this back in 2014. It works both for finding implementations of an interface and finding interfaces implemented by a type.
dang11 months ago
Discussed at the time:<p><i>Interface Upgrades in Go</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8714051">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8714051</a> - Dec 2014 (40 comments)
McMini11 months ago
While the article highlights interface upgrades, it overlooks potential downsides like increased complexity in debugging. Anyone experienced issues with this?
MrBuddyCasino11 months ago
Am I missing something or is this a lot of words to describe the equivalent of the JVMs <i>instanceof</i> &#x2F; type casting operator?
评论 #40767423 未加载
yawz11 months ago
The title needs &quot;2014&quot; to be added.
评论 #40768459 未加载
hmage11 months ago
November 5, 2014
评论 #40768460 未加载