<p>Hello again all.</p>
<p>Thanks for your help on prior questions as I move from super heavy OOP languages to Golang.</p>
<p>Suppose in package 'a', I have:</p>
<pre><code>type Config struct {
path string `json:"-"`
}
</code></pre>
<p>Then in package 'b', I have:</p>
<pre><code>type localConfig struct {
path string `json:"-"`
Server remote.Server
a.Config
}
</code></pre>
<p>I know that when I embed, the methods act on the inner type/the one they're actually bound to. But in those methods, I need to reference 'path'. If I create a new variable of 'localConfig' and set the path in the composite literal, the methods see path as being empty (since it's using a.Config.path rather than b.localConfig.path). What's the easiest way to solve this in Golang? Do I just create a config interface that looks for a method that returns the path? That sounds messy :(</p>
<p>Thanks!</p>
<hr/>**评论:**<br/><br/>Ainar-G: <pre><p>Why don't you just use the <code>path</code> that's inside <code>a.Config</code>?</p></pre>trinchan: <pre><p>I'm not sure about your use case as well, but you can reference the embedded Config path using
localConfig.Config.path
and the localConfig path using
localConfig.path</p>
<p>Working example <a href="http://play.golang.org/p/ZZQ4Nu8ri7" rel="nofollow">here</a>.</p></pre>JakeTheDev: <pre><p>... I can't believe I didn't think of this. Guess i'm still stuck in the inheritance mindset. Thanks!</p></pre>klaaax: <pre><p>You want to use path in both structs then export path with an upper case.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传