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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

How to use interfaces in Go (2012)

49 点作者 abeinstein超过 9 年前

8 条评论

ridiculous_fish超过 9 年前
When navigating unfamiliar Go codebases, it&#x27;s hard to know which types fulfill an interface. Say you have a function that takes an Animal; how do you figure out what to call it with?<p>Or to give a concrete example, say you want to do formatted output to stderr. How is one supposed to determine that os.Stderr is something that can be passed to fmt.Fprintf? Is there a better way than manually comparing method signatures?
评论 #10716073 未加载
评论 #10715896 未加载
EdwardDiego超过 9 年前
&gt; type JavaProgrammer struct { &gt; }<p>&gt; func (j JavaProgrammer) Speak() string { &gt; return &quot;Design patterns!&quot; &gt; }<p><i>shrug</i> At least our language designers trusted our intelligence enough to implement generics.
评论 #10715821 未加载
dtft超过 9 年前
Read this when I was first getting into Go; super helpful and a fun(ish) read for people curious about the language
jdkanani超过 9 年前
Interesting. For pointers and interfaces, in this sample <a href="http:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;Lf8nn_pcYO" rel="nofollow">http:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;Lf8nn_pcYO</a>, `h3.Add2()` should fail as per OP said. I am sure, I am missing something important here. Please help.
评论 #10715864 未加载
nindalf超过 9 年前
The article is a pretty good intro to how to use interfaces. I think some people, especially those not familiar with the language would benefit from reading such articles and learning how to write idiomatic Go code, rather than ask for features that would allow them to write idiomatic $LANGUAGE_THEY_LIKE code.
programd超过 9 年前
Previously on HN:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9320235" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9320235</a><p>The blog post is from 2012.
welder超过 9 年前
&gt; func main() {<p>&gt; animals := []Animal{Dog{}, Cat{}, Llama{}, JavaProgrammer{}}<p>&gt; for _, animal := range animals {<p>&gt; fmt.Println(animal.Speak())<p>&gt; }<p>&gt; }<p>This makes my eyes hurt. In this modern age we still haven&#x27;t learned to create easy to read languages?
评论 #10715796 未加载
评论 #10715683 未加载
评论 #10715789 未加载
评论 #10715904 未加载
slmyers超过 9 年前
I don&#x27;t understand why you would share a three year old post explaining the basics of interfaces in Go.