Help with OOP like feature in Go

blov · · 497 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello All,</p> <p>I just started with Go, and am loving it. Am trying to do something like this in Go,</p> <pre><code>type BaseModel struct { ID int } type User struct { BaseModel Surname string } type Base interface { Get(id int) error } // Also I dont want to have to write this for User struct func (b *BaseModel) Get(id int) { err := sqlx.Get(b, &#34;SELECT * FROM user WHERE id=? LIMIT 0,1&#34;, id) if err != nil { log.Fatal(err) } } func init() { sqlx.Open(&#34;mysql&#34;,&#34;root:1234@tcp(127.0.0.1:3306)/test&#34;) } func main() { ur := User{} ur.Get(1) log.Print(ur) // this should print the user struct, and not the BaseModel struct } </code></pre> <p>I know my problem is here <code>func (b *BaseModel) Get(id int) {</code></p> <p>I want User to inherit the Get method and also should be able to change the table name base on the name of the struct that is calling the Get method <code>err := sqlx.Get(b, &#34;SELECT * FROM **user** WHERE id=? LIMIT 0,1&#34;, id)</code></p> <p>I don&#39;t want to do this <code>xxx.Get(&amp;ur, 1)</code> where <code>xxx</code> can be an orm/sqlx/hood</p> <hr/>**评论:**<br/><br/>nagai: <pre><p>Trying to write OOP in Go is going to leave you very sad and disappointed - there is no concept of inheritance here and if there is it&#39;s a very limited one. Try to figure out the Go way of doing things instead.</p></pre>bogdanbursuc: <pre><p>This is very wrong. Golang doesn&#39;t have OOP and you shouldn&#39;t use it like this.</p> <p>What go offers is embedding and it&#39;s really not anything close to OOP, might look like it&#39;s OOP, but it&#39;s not. </p> <p>What you have is <code>User</code> struct containing a <code>BaseModel</code> field, and your <code>BaseModel.Get</code> is trying to access the user table which makes it really tied to your user anyway.</p> <p>When you do <code>sqlx.Get</code> you are setting the <code>BaseModel</code> field and none of the <code>User</code> fields because the <code>Get</code> method on the <code>BaseModel</code> doesn&#39;t even have access to any of the <code>User</code> fields and doesn&#39;t even know that it&#39;s part of another struct. The <code>Get</code> method is called with the <code>BaseModel</code> as receiver not <code>User</code> receiver. </p></pre>adzcqe: <pre><p>The code is wrong, I know, That is why i comment on it. Its just to make the code work, since there is not table called <code>base_model</code></p> <p>I know Go lang is not OOP, I just want to know if it is possible to do <code>ur.Get(1)</code> in pace of <code>xxx.Get(&amp;ur, 1)</code></p></pre>Hek1t: <pre><p>Does this approach will work in other OOP languages? </p> <p>Method Get () should know about inner structure of retrieving object anyway so you can&#39;t write it once (Except of using reflect, but i think it&#39;s huge overengineering)</p></pre>fudge21: <pre><p>this might be getting at what you&#39;re trying to do but it&#39;s ugly...<a href="https://play.golang.org/p/llAqEKv5b4" rel="nofollow">https://play.golang.org/p/llAqEKv5b4</a></p></pre>watr: <pre><p><a href="http://hackthology.com/object-oriented-inheritance-in-go.html" rel="nofollow">You are looking for this.</a></p></pre>womplord1: <pre><p>Heresy</p></pre>

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

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