duanjiyun7391 2014-07-19 11:27
浏览 75

Go的“插件系统”

I'm looking for an equivalent of Architect for the Go language.

With Architect, modules expose "plugins". Plugins can specify dependencies, and export an API to allow interaction with other plugins. To start an application instance, you specify a list of plugins. Dependencies are resolved, and plugins are loaded (instantiated) in order.

Since each application creates a single instance of each plugin, multiple applications can be started in the same process without colliding.

Edit: I don't need the other modules to be loaded dynamically.

  • 写回答

1条回答 默认 最新

  • duanrou5680 2014-07-20 15:51
    关注

    I don't of a package that does that, but have some thoughts on how to do that - hope it'll help.

    • Use a build tag for each plugin.
    • Have each plugin (file) specify in a special comment/variable its dependencies
    • Run a pre build step that generate order of initialization (toplogical sort, fail on cycles). The output is a go file which is the function called by the plugin system initialization.
    • Have Registry and Plugin interfaces, probably something like:

      type Registry {
          // Register registers a plugin under name
          Register(name string, plugin *Plugin) error
          // Get plugin by name
          Get(name string) (*Plugin, error)
      }
      
      // Global Registry
      var GlobalRegistry Registry
      
      type Plugin interface {
          // Init is called upon plugin initialization. Will be in dependency order
          Init(reg Registry) error
          // Execute plugin command
          Exec(name string, args... interface{}) (interface{}, error)
      }
      
    • Run go build -tags plugin1,plugin2
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分