Import based on case statement

polaris · · 470 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi</p> <p>I&#39;ve got a flag set called &#34;brand&#34; 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&#39;ve tried creating a package called jobs with two sub folders named after the brands, but it&#39;s not working.</p> <p>Can anyone recommend a good way to achieve what I&#39;m after? Ideally I&#39;d like to call external.CollectData and it uses the brand&#39;s version of that method. </p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><p>It&#39;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&#39;s call it <code>Interface</code>) in <code>yourpackage/brand</code> that describes the interactions with a &#34;brand&#34; (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&#39;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&#39;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 &#34;Ruby&#34; 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 &#34;an official plugin mechanism&#34; and the second is mentioned as &#34;use something like hashicorp/go-plugin&#34;.</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&#39;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 &#34;brand&#34; 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

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