是否可以动态加载Go代码? [英] Is it possible to dynamically load Go code?

查看:135
本文介绍了是否可以动态加载Go代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我想根据运行时可用的信息动态加载Go包(或不包含)。

As the title says I want to dynamically load a Go package (or not) based on information only available at run time.

目标是让用户通过添加新的本地脚本命令的自定义程序包来扩展程序。目前,每当我添加新命令或想要禁止某些命令时,我需要编辑程序并重新编译,而如果我可以创建某种类型的DLL或类似的命令,那么我可以创建一个导入脚本命令来搜索并加载一个命名的命令库。

The objective is to allow the user to extend the program via custom packages that add new native script commands. Currently every time I add new commands or want to disallow some commands, I need to edit the program and recompile, whereas if I could make some kind of dll or the like then I could create a "import" script command to search for and load a named command library.

好奇的程序是基于自定义命令的脚本库,我用它来处理各种各样的事情。

For the curious the program in question is custom command based scripting library that I use for all kinds of things.

我提前做了一些搜索,结果看起来不太好,但我找不到明确的答案。

I did some searching ahead of time, and the results don't look good, but I could not find a clear no.

推荐答案

Go还不支持动态库。 Elias Naur最近发布了一些补丁程序,但他们还没有经过审查,他们不太可能被纳入Go 1.2。您可以阅读关于Google网上论坛的讨论:

Go does not support dynamic libraries yet. Elias Naur has recently published some patches, but they have not been reviewed yet and it is unlikely that they will be included in Go 1.2. You can read the discussions on Google Groups:

  • https://groups.google.com/d/topic/golang-nuts/o0VTTqC8hOU/discussion
  • https://groups.google.com/d/topic/golang-nuts/P05BDjLcQ5k/discussion

据我所知,这是关于该主题的最新讨论。

As far as I know, that are the most recent discussions about that topic.

还有另一种方法。您可以在单独的进程中启动插件,并使用 net / rpc 软件包与您的main应用程序。这也允许你动态地启动/停止/重新编译单独的插件,它的优点是一个坏的插件不能让程序崩溃。 Go擅长网络交流,你只需要善用它。

There is however another approach. You can start your plugins in separate processes and use the net/rpc package to communicate with your main app. This also allows you to dynamically start / stop / recompile separate plugins and it has the advantage that a bad plugin can not crash your program. Go excels at network communication, you just have to make good use of it.


我需要编辑程序并重新编译,

I need to edit the program and recompile,

您还可以考虑编写一个小脚本,监视当前目录中的更改(使用fsnotify)并执行go build,然后重新启动程序。我在本地开发期间在我的一些Web项目中使用了这种方法,并且工作正常。我无法观察任何编译时间,而且我在切换和刷新浏览器窗口方面速度非常快。我的Python开发周期中,必须重新启动解释器,并且每次更改都必须重新导入所有模块(并且这可能需要大型项目中的大量时间!),与Go相比,感觉非常笨拙。

You can also consider writing a small script that watches for changes in the current directory (using fsnotify) and executes "go build" followed by a restart of your program. I use this approach on some of my web projects during local development and it works fine. I am not able to observe any compilation times and I am quite fast at switching and refreshing my browser window. My Python development cycle, where the interpreter has to be restarted and all modules have to be reimported on every change (and that might take a significant time in larger projects!), feels really clumsy in comparison to Go.

这篇关于是否可以动态加载Go代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆