What is the purpose of implicit implementation of interfaces? (beginner)

polaris · · 494 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello, I&#39;m a beginner at the Go language.</p> <p>I understand an interface is a fixed set of methods.</p> <p>And if you define types and those types have receiver methods which match the signature of the interface then those types are automatically implementations of that interface.</p> <p>What I don&#39;t understand is, what is the point? What are the benefits of this? I&#39;ve already had to re-define the methods on the types, so the hard work is done. What do I gain now these methods match up with the methods in the interface?</p> <p>Thanks</p> <hr/>**评论:**<br/><br/>theGeekPirate: <pre><p>Think of each part of your application as a lego piece.</p> <p>Interfaces are the portions which stick out and have holes in them, which allow you to attach different pieces together, making your entire application incredibly modular.</p> <p>For instance, take a look at the Mapper interface here <a href="https://github.com/fogleman/nes/blob/master/nes/mapper.go" rel="nofollow">https://github.com/fogleman/nes/blob/master/nes/mapper.go</a></p> <p>In the NewMapper function, you can see all of the different mappers which can be used.</p> <p>In order to use separate mappers (used by different games), one simply has to create a type which has all of those methods, and they can then be magically used anywhere where that interface is required.</p> <p>Here&#39;s an example of one of those mappers <a href="https://github.com/fogleman/nes/blob/master/nes/mapper3.go" rel="nofollow">https://github.com/fogleman/nes/blob/master/nes/mapper3.go</a></p></pre>weberc2: <pre><p>Interfaces are for code reuse. They separate the &#34;signature&#34; of an object from the implementation.</p> <p>If you&#39;re writing a library function that operates on a sequence of items, you probably don&#39;t care that the sequence is an array list or a linked list. You may not even care that it has a finite number of elements. In this case, you just care that it implements:</p> <pre><code>type Sequence interface { Next() *Element } </code></pre> <p>Then you can write your library function, and it will work for all kinds of sequences (ArrayLists, Arrays, LinkedLists, etc), rather than having to write one library function for every conceivable kind of sequence.</p></pre>TheMerovius: <pre><blockquote> <p>I&#39;ve already had to re-define the methods on the types, so the hard work is done.</p> </blockquote> <p>You don&#39;t necessarily have to redefine the methods. You can use field-embedding and have some or all of the defined methods promoted.</p> <p>But the real usecase is that package A doesn&#39;t need to know about interfaces that package B defines. That means a) less imports, thus faster compiles and prevents dependency-cycles and b) more importantly that you can define your own interfaces for <em>other</em> people&#39;s types and thus, for example, easily generalize them or mock them out. For example quite a lot of people define an interface that encapsulates the most important methods of <a href="http://godoc.org/log#Logger" rel="nofollow">*log.Logger</a>. Now, the log package itself doesn&#39;t define an interface, but you can take an interface with the relevant methods and then you (or users of your package) can either put a *log.Logger in it or define their own loggers.</p> <p>You should also try to not look at language features isolated in go, but instead look how they compose with other features. go is explicitly designed to be orthogonal. A good example is the type-embedding mentioned above. It combines well with interfaces to let you easily overwrite methods of an interface.</p> <p>Lastly, you are approaching the problem from the wrong side: The opposite of structural implementation would be, to explicitly state which interfaces you are implementing. But that would actually be <em>more</em> work, so you would need to <em>add</em> to the language to do that. So rather than asking &#34;what&#39;s the point of having structural typing&#34; you should ask &#34;what&#39;s the point of specifying which interfaces I implement&#34;? In particular &#34;I&#39;ve already had to implement the methods, why should I need to do more?&#34;. :)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

494 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传