Stuck in OO-mindset, how would I solve this?

polaris · · 626 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m probably stuck in an OO mindset seeking inheritance, but is there any way to have a method work on multiple structs beyond embedding? I know that I can embed another struct so that the methods are promoted for the original struct but they only work on the inner struct. Is there any way I can make a method that works on the outer struct for both (like if I wanted to json.Marshal the entire struct).</p> <p>Example here (I&#39;d like to not have 2 encode functions): <a href="http://play.golang.org/p/-Hr_zqU-o0" rel="nofollow">http://play.golang.org/p/-Hr_zqU-o0</a></p> <p>Like I mentioned, I have a feeling i&#39;m just returning to my OOP stuff and that I should make a function that has no receiver which I&#39;d then pass the structs into for encoding, etc. How would you solve this?</p> <p>Thanks!</p> <hr/>**评论:**<br/><br/>nw0428: <pre><p>First of all, it will make your life easier to not think in &#34;methods&#34; go has functions not methods. You can have your function take in an interface. <a href="https://godoc.org/golang.org/x/tools/go/types#Type" rel="nofollow">https://godoc.org/golang.org/x/tools/go/types#Type</a></p> <p>This allows your function to take in any struct.</p> <p>edit: Said Type meant empty interface &#34;interface{}&#34; Maybe like this: <a href="http://play.golang.org/p/Sad8Y1Ibd7" rel="nofollow">http://play.golang.org/p/Sad8Y1Ibd7</a></p></pre>JakeTheDev: <pre><p>I guess just putting the json.Marshal in there was a bad example since I do a ton of other things to it. But I see what you&#39;re getting at. I guess I&#39;m just so used to wanting to build an object to pass things to. </p> <p>Thanks for your help</p></pre>nw0428: <pre><p>The other thing I should mention is that it sounds like you are looking for an interface. <a href="https://gobyexample.com/interfaces" rel="nofollow">https://gobyexample.com/interfaces</a></p></pre>nuunien: <pre><p><a href="https://golang.org/doc/effective_go.html#methods" rel="nofollow">uh?</a></p></pre>echophant: <pre><p><a href="http://play.golang.org/p/aTJxLgB7Gk" rel="nofollow">Working with your example</a></p> <p>For the example you provided, since marshaling JSON works on type interface{} anyway, you hardly even need to make a function for it at all, because it will accept any struct. One thing to note: encoding to JSON will only use exported (capitalized) fields, so I&#39;ve capitalized your members accordingly. For examples not strictly about marshaling JSON, you&#39;ll want to use <a href="https://gobyexample.com/interfaces" rel="nofollow">interfaces</a>.</p></pre>JakeTheDev: <pre><p>I suppose my response to <a href="/u/nw0428" rel="nofollow">/u/nw0428</a> is also applicable here too.</p> <p>Gotta get out of OOP -- thanks!</p></pre>

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

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