douzhaobo6488 2015-07-29 21:02
浏览 35
已采纳

如何强制golang将文件链接到我的二进制文件中?

I want to create a module library where a files can be added and will be part of my binary.

For example, in my package main I have:

type InitFunc func(params DriverParams) (Driver, error)

func Register(name string, f InitFunc) {
}

Then I would like someone to add a file in the modules directory that calls Register().

Subsequently my main package will call all the functions that have registered themselves.

This way, my main package has no prior knowledge of the modules that will be added.

How do I accomplish this in golang?

  • 写回答

1条回答 默认 最新

  • dongshen7407 2015-07-29 21:35
    关注

    In short - you cannot. Go links everything statically and does some optimizations so the module you installing may not even be compiled if you do not reference it explicitly from main. Such limitation makes people suffer and they do this - the plugins are just normal Go applications communicating with the main app via RPC.

    It may sound weird (when one part of your app talks to another via TCP stack), but if you think about a bit more then it actually gives you quite strong confidence that plugin will do no harm to the application. For example, when the plugin crashes, the rest of the application, most likely, will survive.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里