dongwu9063 2017-04-03 09:47
浏览 31
已采纳

去插件变量初始化

This is a general question about Go Plugin initialization.

I want to make a certain package in a Go program to a Go Plugin.

The package (say mypackage) has a variable which is initialized with a function call from a certain package in the executable. (E.g. a variable of type interface Logger which is to be initialized by a logger.CreateLogger function.)

In order to make mypackage a plugin, I need to create a main package, embed mypackage inside main, and export an api Init in package main which accepts a function which can get me a logger.

I want to do this so as to reduce the dependencies of my plugin. The mypackage plugin should depend on the interface Logger rather than Logger's implementation.

Now the problem is the initialization, in the case of executable, the initialization could have happened as below:

package mypackage

var dlog = logger.CreateLogger("mypackage")

And the logger can be used in any func init() function of mypackage.

After converting to a plugin, it can't be initialized like this. It has to be initialized at a later point after the main.Init is invoked.

The func init is invoked when the plugin is Opened, so it cannot be used to initialize variables.

Only solution seems to be a creating a function per package, and invoke it from an exported function in main package.

package mypackage

var dlog Logger

func Init(f createLoggerType){
   dlog = f()
   yetanotherpackage.Init(f)
}

package main

func Init(f createLoogerType) {
    mypackage.Init(f)
    anotherpackage.Init(f)        
}

Is there a better way to initialize? I tried checking github.com/facebookgo/inject but couldn't figure out how it can be used in case of plugins.

  • 写回答

1条回答 默认 最新

  • doune1000 2017-04-03 10:07
    关注

    There is nothing wrong with your solution, but if you want to be able to use it in variable declarations and package init() functions, this is how you can do it. I also think it's easier to use the plugin this way, but this requires a common, shared package between your main app and the plugin.

    One option would be to create a "store" package, which could store factory functions or pre-created loggers.

    Let's say the Logger interface definition is in mylogger:

    package mylogger
    
    type Logger interface { Log(string) }
    

    The store for example:

    package store
    
    import "mylogger"
    
    var LoggerFactory func(string) mylogger.Logger
    

    And in your main app initialize it before you load / open the mypackage plugin:

    package main
    
    import "store"
    
    func main() {
        store.LoggerFactory = logger.CreateLogger
    
        // Now proceed to load / open mypackage plugin
    }
    

    Then the mypackage plugin may look like this:

    package mypackage
    
    import "store"
    
    var dlog = store.LoggerFactory("mypackage")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件