I have a little doubt with this part of "Learn Go in Y Minutes"

blov · · 892 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>From <a href="http://learnxinyminutes.com/docs/go/">http://learnxinyminutes.com/docs/go/</a></p> <pre><code>// Define Stringer as an interface type with one method, String. type Stringer interface { String() string } // Define pair as a struct with two fields, ints named x and y. type pair struct { x, y int } // Define a method on type pair. ****Pair now implements Stringer.**** func (p pair) String() string { // p is called the &#34;receiver&#34; // Sprintf is another public function in package fmt. // Dot syntax references fields of p. return fmt.Sprintf(&#34;(%d, %d)&#34;, p.x, p.y) } </code></pre> <p>My problem is the part inside ****. </p> <p>How does pair implement <strong>Stringer</strong>?<br/> It&#39;s the first time I&#39;m learning Go, and to me it looks like pair now implements String.</p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><blockquote> <p>How does pair implement Stringer? It&#39;s the first time I&#39;m learning Go, and to me it looks like pair now implements String.</p> </blockquote> <p>pair get&#39;s the Method <code>String() string</code>. A <code>Stringer</code> is anything that has a method <code>String() string</code>. Thus, pair now implements the <code>Stringer</code> interface.</p> <p>In go, that&#39;s just how interfaces work: You define a set of methods and you can then use it as a type (e.g. for a variable). Anything that has these methods with the correct <del>type</del> signature, will implement the interface.</p> <p>You can also look at <a href="http://play.golang.org/p/tzP4mO14Gd">this</a> and play around a bit. When you remove or comment out lines 47 and 49 (and 33), it also compiles (but look at the compiler-error first).</p> <p>[edit] s/numbers/methods/ weird freudian typo…</p></pre>drvd: <pre><p>Maybe you should take the Tour of Go (<a href="http://tour.golang.org/welcome/1">http://tour.golang.org/welcome/1</a>) first.</p></pre>bbrazil: <pre><p>The convention is that when you define such a method, you mention which interface it&#39;s part of. So by implementing String(), you&#39;re implementing (in this case all of) the Stringer interface.</p></pre>jerf: <pre><p>As an exercise, take this exact file, and:</p> <ul> <li>Add <code>var _ Stringer = pair{1, 2}</code> to the top level. This essentially creates an assertion that <code>pair</code> implements <code>Stringer</code>.</li> <li>Compile to see that it&#39;s working.</li> <li>Add another method to the Stringer interface, say, <code>NotImplemented()</code>.</li> <li>Compile again. Observe the error message.</li> <li>Now, fix it by adding something to <code>pair</code>.</li> </ul> <p>And, well, that&#39;s pretty much what it takes to implement interfaces in Go. The only slightly tricky thing is that you have to match pointer or non-pointer receivers on all the methods, and when directly assigning in to an interface, you have to match the correct level of pointerness. (Once it&#39;s &#34;in&#34; the interface, it&#39;s all opaque to you and you can stop thinking about it.)</p></pre>binaryblade: <pre><p>This is the fundamental nature of interfaces, you never have to declare that you support an interface. The fact that your &#34;object&#34; implements all the methods of the interface means that the object implements the interface. It is an automagic property. Pair implements a method called String() string. This is the only method required to satisfy the Stringer interface, therefore, pair can be a stringer. You can say, it walks like a duck, talks like a duck therefore it is a duck. </p></pre>umegastar: <pre><p>Thanks everybody, now I understand.</p></pre>dmikalova: <pre><p>By implementing String, pair satisfies the interface for Stringer, and thus implements Stringer.</p> <p>It&#39;s that simple :)</p></pre>

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

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