<p>Hi.</p>
<p>I am currently trying to call a function I have defined in my "main" package from another package I included from "main".</p>
<p>With functions in other packages you can do somepackage.Somefunction(), but I can't get it to work the other way around, FuncInMain() doesn't work and main.FuncInMain() doesn't work either.</p>
<p>How would I do this, or isn'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'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's command it received the code from that package will be executed.</p>
<p>(RegisterCommand() takes a function pointer that'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'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 "callback hell" 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'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'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't two packages, but does show the closure functionality on a call.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传