Call a function in main package from another package

agolangf · · 440 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi.</p> <p>I am currently trying to call a function I have defined in my &#34;main&#34; package from another package I included from &#34;main&#34;.</p> <p>With functions in other packages you can do somepackage.Somefunction(), but I can&#39;t get it to work the other way around, FuncInMain() doesn&#39;t work and main.FuncInMain() doesn&#39;t work either.</p> <p>How would I do this, or isn&#39;t there really a way?</p> <hr/>**评论:**<br/><br/>SSoreil: <pre><p>You can pass the function from main to your package (probably). It sounds like a really odd thing to need to do but it does sound like you want a function argument.</p></pre>justinisrael: <pre><p>The design sounds backwards. A main package should consume libraries. Libraries should not depend on a main. That would break the idea of them being a library. What kind of function do you actually want to call in your main? There is most definitely a better way to do it. </p></pre>TheRealHellcat: <pre><p>The code receives commands from the outside and my main package has a function RegisterCommand() where I can register a command from any other place in the code so when a registered command is received it&#39;ll be executed.</p> <p>Now I have an extension package, that acts a bit like plugins, where I want to put codefiles in that can cal RegisterComman() so that when it&#39;s command it received the code from that package will be executed.</p> <p>(RegisterCommand() takes a function pointer that&#39;s called when the given command is received).</p></pre>zacheryph: <pre><p>You move your whole command registration to its own library as well. </p></pre>justinisrael: <pre><p>Or have the library provide a setup function that your main can call into, to allow it to register functions. Or have your library provide a <code>Commands()</code> function that your main can call to get a list of commands to register. Basically anything besides having your library attempt to call functions directly in main</p></pre>TheRealHellcat: <pre><p>Yes, that&#39;s what I ended up doing - work as expected now :)</p></pre>thepciet: <pre><p>A way is the use of anonymous functions (function as a variable):</p> <pre><code>package mylib func DataUpdate(data interface{}, callback func() error) error </code></pre> <p>The callback is provided by the library user. This is used when there are multiple possible behaviors for the library, such as a different callback for a phone vs laptop.</p> <p>There is a reason &#34;callback hell&#34; is a term, careful. Generally calling package main functions sounds dirty, cause now everything is all tied together and potentially tougher to learn/update/fix/maintain.</p></pre>im7mortal: <pre><p>You can&#39;t do it.</p> <p>You can only pass function from <em>main</em> to function from your library. </p> <p>Anyway you have to define a signature of the function in your library. </p> <p>Check a middleware conception.</p> <p>There are some example. It&#39;s just conception.</p> <p><a href="https://play.golang.org/p/CbDtluiPt5" rel="nofollow">example</a></p></pre>thepciet: <pre><p>You can definitely get anonymous functions / closures from libraries.</p> <p><a href="https://play.golang.org/p/sp4GiYATZZ" rel="nofollow">https://play.golang.org/p/sp4GiYATZZ</a></p> <p>Just gotta return a func. This isn&#39;t two packages, but does show the closure functionality on a call.</p></pre>

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

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