is it possible to extend a type ?

blov · · 469 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I may be wording the title incorrectly , but it is the closest thing I could think of when trying to describe what I am trying to do. I am fairly new to Go , so if I am way off on this and there is a better way to do this please let me know.</p> <p>what I would like to be able to do is leverage an existing type in an external library in my application, but add a few fields to it. the reason I would like to do this is because it does not make sense to me create the exact same type with its 20 fields plus my 2 new fields when the 20 fields already exist. Some sudo code below of what I was thinking that does not work.</p> <pre><code>type ExtendedType *externallib.SomeType { someextrafield string } </code></pre> <p>essentially I would have use of all of the fields that the externallib.SomeType has plus my own. If this makes no sense and there is a better way to accomplish what I want to do please let me know.</p> <hr/>**评论:**<br/><br/>alayton: <pre><pre><code>type ExtendedType struct { *externallib.SomeType someExtraField string } </code></pre> <p>This is also how you would add new methods to a type from a different package. More details here: <a href="https://golang.org/doc/effective_go.html#embedding" rel="nofollow">https://golang.org/doc/effective_go.html#embedding</a></p></pre>MultiChef: <pre><p>is there a way to do this so that the external type is merged with the extended type rather than nested? the main reason I am asking is because i will be decoding json into the struct and i would rather not have the original fields in a separate nested object.</p></pre>alayton: <pre><p>When you embed a type, its members are treated as members of the extended type - they&#39;re effectively merged, as you say. You can also access them as if they were nested, but that&#39;s primarily only useful if you override some of the original members in your extended type and then want to access the embedded member later.</p></pre>MultiChef: <pre><p>ok that makes sense, however maybe I am still missing something. in this example <a href="https://play.golang.org/p/nxP2n97TwU" rel="nofollow">https://play.golang.org/p/nxP2n97TwU</a> shouldn&#39;t this be working? </p></pre>: <pre><p>[deleted]</p></pre>MultiChef: <pre><p>ok this makes sense now. thank you for the help!</p></pre>Ayiga: <pre><p>I accidently deleted the post because the Reddit mobile app showed I had posted it 3 times. :/</p> <p>Here&#39;s the json example of the extension&#39;s effect: <a href="https://play.golang.org/p/fYCBmKUUqM" rel="nofollow">https://play.golang.org/p/fYCBmKUUqM</a></p></pre>Yojihito: <pre><p>Why is &#34;{hello}&#34; nested in braces and &#34;testing&#34; is not in the first fmt.println?</p></pre>TheMerovius: <pre><p>You are embedding via pointer and that pointer is nil. <code>test.field1</code> is equilalent to <code>test.SomeType.field1</code> and <code>test.SomeType</code> is nil. Do this instead (it&#39;s better from a memory-layout perspective anyway): <a href="https://play.golang.org/p/C9kC3tlS91" rel="nofollow">https://play.golang.org/p/C9kC3tlS91</a></p></pre>9nut: <pre><p>you&#39;re missing the initialization for the embedded type: <a href="https://play.golang.org/p/Q4iFGRlc_g" rel="nofollow">https://play.golang.org/p/Q4iFGRlc_g</a></p></pre>TheMerovius: <pre><p>See the documentation of the json marshalling:</p> <blockquote> <p>Anonymous struct fields are usually marshaled as if their inner exported fields were fields in the outer struct, subject to the usual Go visibility rules amended as described in the next paragraph. An anonymous struct field with a name given in its JSON tag is treated as having that name, rather than being anonymous.</p> </blockquote></pre>iends: <pre><p>Use an embedded type. The new struct will still adhere to the interface. See <a href="https://www.goinggo.net/2014/05/methods-interfaces-and-embedded-types.html" rel="nofollow">https://www.goinggo.net/2014/05/methods-interfaces-and-embedded-types.html</a></p></pre>kurin: <pre><p>You do this with type embedding: </p> <p><a href="https://golang.org/doc/effective_go.html#embedding" rel="nofollow">https://golang.org/doc/effective_go.html#embedding</a></p></pre>shovelpost: <pre><p>Wrap the external type with your type.</p> <pre><code>type ExtendedType struct { *externallib.SomeType someExtraField string } </code></pre> <p>Helpful read: <a href="https://nathany.com/good/#composition-by-example" rel="nofollow">Go Object Oriented Design</a></p></pre>

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

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