Methods "overriding" in Go

agolangf · · 516 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey,</p> <p>Recently started programming in Go and one thing I was wondering of was how can I provide some common functionality that can be shared between different types, e.g. similar to what OOP languages have where you can basically override a method in a subclass, but use the parent class methods to perform some common tasks as well.</p> <p>One way I was thinking of how to solve this is to use interfaces and a generic struct which implements the interface. The generic struct can be embedded in other types, so this way I can &#34;override&#34; just the methods I need to.</p> <p>Here is what I&#39;ve got so far. The code works as expected and does what I need to, but I do have some concerns whether this approach is considered as idiomatic.</p> <ul> <li><a href="https://play.golang.org/p/IhlvbgknnA" rel="nofollow">https://play.golang.org/p/IhlvbgknnA</a></li> </ul> <hr/>**评论:**<br/><br/>freeformz: <pre><p>Embedding != inheritance. So for instance the DummyTask does not contain a Result string. It contains a GenericTask that contains a Result string. So the t.Result = &#34;success&#34; is really t.GenericTask.Result = &#34;success&#34;, go just wraps that up for you. This is embedding and composition vs inheritance.</p> <p>Interfaces however define behaviors that structs can implement. So given your example anything than implements both functions <code>GetResult() (string,error)</code> and <code>Process() error</code> can be passed as a Task to another function.</p> <p>Generally speaking you would implement the shared functionality in functions that take an Interface value as an argument. Those functions then use the <em>just</em> the interface of the value provided to accomplish the goals.</p> <p>It may take a bit to really get used to the differences, especially if you were doing a lot of OOP before.</p></pre>r00ky: <pre><p>I think something like this is what your are looking for:</p> <ul> <li><a href="https://play.golang.org/p/PiBd2PdLpm" rel="nofollow">https://play.golang.org/p/PiBd2PdLpm</a></li> </ul> <p>The public Process function encapsulates the actual implementation for different types.</p></pre>qcoh: <pre><p>Overriding like you did is exactly how it&#39;s done.</p> <p>I would consider a concrete struct intended to be abstract by putting panics in methods rather ugly. If you don&#39;t want GenericTask.Process() to be called ever, I would rather do something like this (someone please correct me if I&#39;m wrong):</p> <pre><code>type Task interface { Process() GetResult() (string, error) } type ResultBase struct { Result string } func (rb *ResultBase) GetResult() (string, error) { return rb.Result, nil } type DummyTask struct { ResultBase } func (dt *DummyTask) Process() { dt.Result = &#34;success&#34; } </code></pre></pre>

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

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