Go Plugin变量初始化 [英] Go Plugin variable initialization

查看:160
本文介绍了Go Plugin变量初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于Go插件初始化的一般问题。



我想在Go程序中将某个程序包制作为Go插件。



包(例如 mypackage )具有一个变量,该变量使用可执行文件中的某个包的函数调用进行初始化。 (例如,一个接口Logger 类型的变量,它将被一个 logger.CreateLogger 函数初始化。)



为了制作 mypackage 插件,我需要创建一个 main 包,在 main 内嵌入 mypackage ,并导出api Init 包含在 main 中,它接受一个可以让我记录的函数。



我想这样做以减少插件的依赖关系。 mypackage 插件应该依赖于接口Logger 而不是Logger的实现。



现在问题是初始化,在可执行的情况下,初始化可能发生如下:

  package mypackage 

var dlog = logger.CreateLogger(mypackage)

这个记录器可以用于 mypackage 的任何 func init()函数中。



转换为插件后,无法像这样初始化。必须在调用 main.Init 之后的某个时间点初始化。 c> func init 在插件打开时被调用,所以它不能用于初始化变量。



只有解决方案似乎是创建每个包的函数,并从 main 包中的导出函数中调用它。

  $ b $ var dlog Logger 

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


$ main

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

有没有更好的初始化方法?我试着检查 github.com/facebookgo/inject ,但无法弄清楚它是如何用于插件的。

解决方案

您的解决方案没有问题,但是如果您希望能够在变量声明和包中使用它 init()函数,这是你如何做到的。我也认为用这种方式使用插件更容易,但是这需要在主应用程序和插件之间使用共同的包。



一种选择是创建一个store包,它可以存储工厂函数或预先创建的记录器。



比方说 Logger 接口定义 mylogger

 软件包mylogger 

类型Logger接口{Log(string)}

store 例如:

 包存储

导入mylogger

var LoggerFactory func(string)mylogger.Logger

然后在您的主应用程序中初始化 之前加载/打开 mypackage 插件:

  package main 
$ b $ importstore

func main(){
store.LoggerFactory = logger.CreateLogger

//现在继续加载/打开mypackage页面lugin
}

然后 mypackage 插件可能看起来像这样:

 包mypackage 

导入store

var dlog = store.LoggerFactory(mypackage)


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.

解决方案

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")

这篇关于Go Plugin变量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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