<p>Hi</p>
<p>I've got a flag set called "brand" on running my application. Depending on the brand passed through I want to import a different file. I want a method such as CollectData to do something different depending on brand.</p>
<p>I've tried creating a package called jobs with two sub folders named after the brands, but it's not working.</p>
<p>Can anyone recommend a good way to achieve what I'm after? Ideally I'd like to call external.CollectData and it uses the brand's version of that method. </p>
<hr/>**评论:**<br/><br/>TheMerovius: <pre><p>It's not possible to do this, as what you import is a compile-time decision and flags are only defined at runtime.</p>
<p>The best you can do is, what <a href="/u/megadyne" rel="nofollow">/u/megadyne</a> suggests (the best solution for when you have complete control), or what database/sql does. For this:</p>
<ul>
<li>Define an interface (let's call it <code>Interface</code>) in <code>yourpackage/brand</code> that describes the interactions with a "brand" (whatever that may be)</li>
<li>Have a <code>Register(name string, brand Interface)</code> function in <code>yourpackage/brand</code> which saves <code>brand</code> under <code>name</code> in a <code>map[string]Interface</code></li>
<li>Make packages <code>yourpackage/brand/a</code> and have them call <code>brand.Register</code> with a custom implementation in <code>init</code>.</li>
<li>Include all the subpackages and <code>yourpackage/brand</code> in your <code>main</code> package and then use the <code>brand.Interface</code> you get from the map.</li>
</ul>
<p>But, in any case, all code that could be used in a program must be imported from it's <code>main</code> package at compile time. If that is not something you are willing to live with (e.g. you want customers to be able to provide their own implementation), you'll have to use a scripting language, wait for an official plugin mechanism, or use something like <a href="http://godoc.org/github.com/hashicorp/go-plugin" rel="nofollow">hashicorp/go-plugin</a>.</p></pre>jot_gabbi: <pre><p>Thanks for that, really useful! Starting to like golang more and more each day, just trying to get out of "Ruby" mode!</p></pre>trisjpheck: <pre><p>Non-scripting languages allow you to <em>load</em> pre-compiled code at runtime. </p>
<p>Or you can even compile code on the fly before loading it.</p></pre>TheMerovius: <pre><p>The first is mentioned as "an official plugin mechanism" and the second is mentioned as "use something like hashicorp/go-plugin".</p></pre>trisjpheck: <pre><p>Right, but you seemed to offer either scripting languages or workarounds in Go, whereas I am saying that there are other tools that might be better suited than Go if dynamic code loading is involved. </p>
<p>But, maybe using Go is a requirement in OP's case, and you are indeed giving interesting options.
Note that your link to hashicorp/go-plugin is invalid, it should probably be <a href="https://github.com/hashicorp/go-plugin" rel="nofollow">https://github.com/hashicorp/go-plugin</a>. It reminds me a little of CORBA and co. (not saying this is bad, though).</p></pre>megadyne: <pre><p>You could make a (global) variable for that function and choose your "brand" function at the start of the program.</p>
<p><a href="https://play.golang.org/p/yVfYhKz6dK" rel="nofollow">Something like this.</a></p></pre>jot_gabbi: <pre><p>This worked perfectly, thanks <a href="/u/megadyne" rel="nofollow">/u/megadyne</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传