<p>I apologize if this is a simple question, but I am coming from a Java / C++ background and get a little tripped up by Go.</p>
<p>So here is my question. Lets say we have the following:</p>
<pre><code>myProgram/
main.go
cart/
cart.go //Contains only interface / structs
packageWhereInterfacesAreImplemented/
</code></pre>
<p>How could I write to the interfaces in cart from packageWhereInterfacesAreImplemented? The idea being that if I want to change how my cart is implemented then all I do is change the package packageWhereInterfacesAreImplemented to whatever I want.</p>
<p>Normally I just implement the interfaces and then the compiler links it all up, but Go uses implicit implementation. Would I just write go files that implement the specific interfaces and that will do it?</p>
<hr/>**评论:**<br/><br/>nliadm: <pre><p>Yes, interfaces are satisfied implicitly.</p>
<p>You can have the compiler check the interface for you if you assign to a discard variable of the type you want:</p>
<p><code>
var _ io.Writer = (*myWriter)(nil)
</code></p></pre>Fireynis: <pre><p>OK, interesting, thank you.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传