golang:如何卸载已经加载的"go plugin"1.8 [英] golang: how to unload an already loaded "go plugin" 1.8

查看:272
本文介绍了golang:如何卸载已经加载的"go plugin"1.8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

go1.8及更高版本,go支持创建和加载插件.

go1.8 onwards, go supports to create and load a plugin.

但不支持卸载插件.

插件是在运行时加载的模块,是否可以卸载模块?

a plugin is a module loaded at runtime, is it possible to unload a module?

如果无法卸载模块,那么在应用程序级别上卸载插件/使其无法使用但仍在内存中的最佳方法是什么?

if not possible to unload a module, what is the best that can be done at application level to unload a plugin/make it unusable but still in memory?

推荐答案

Go不支持卸载插件.但是您可以按照您的建议将其禁用.通常,插件会定义一个包含有关插件信息的结构.您可以从工厂函数以一个众所周知的名称返回它(例如, awesome.so 包含 AwesomePlugin ).您可以在结构中包含的一项功能是一种禁用对插件的访问的方法.您可以执行以下操作:

Go doesn't support unloading a plugin. But you can, as you suggest, disable it. Commonly a plugin would define a struct containing the information about the plugin. You might return this from a factory function with a well-known name (e.g. awesome.so contains AwesomePlugin). One of the items you could include in the struct would be a method to disable access to the plugin. You could do something like this:

type MyPlugin struct {
    Name string
    Enable func() error
    Disable func() error
}

然后在插件本身中,您将执行以下操作:

Then in the plugin itself you'd do something like this:

var (
    awesomeEnabled bool
)

func AwesomePlugin() *myplugin.MyPlugin {
    return &myplugin.MyPlugin{
        Name: "AwesomePlugin",
        Enable: func() error {
            println("Enabling AwesomePlugin")
            awesomeEnabled = true
            return nil // or do something more complex that could error
        },
        Disable: func() error {
            println("Disabling AwesomePlugin")
            awesomeEnabled = false
            return nil // or do something more complex that could error
        },
    }
}

然后加载,启用和禁用它的代码将类似于:

Then the code to load it, enable it, and disable it would be something like:

awesomePlugin, err := plugin.Open("awesome.so")
if err != nil {
    panic("Can't load plugin: " + err.Error())
}

sym, err := awesomePlugin.Lookup("AwesomePlugin")
if err != nil {
    panic("Can't find symbol: " + err.Error())
}

awesomeFactory := sym.(func() *myplugin.MyPlugin)
awesome := awesomeFactory()

println("Loaded " + awesome.Name + " plugin")

err = awesome.Enable()
if err != nil {
    panic("Can't enable plugin: " + err.Error())
}

// Do some stuff

err = awesome.Disable()
if err != nil {
    panic("Can't enable plugin: " + err.Error())
}

在运行您可能定义的任何其他功能之前,您将使插件中的代码看起来是否启用了插件.

You'd have the code in the plugin look to see if the plugin is enabled or not before running any other functions you might define.

然后,运行它,我们得到如下输出:

Then, running it, we get output like:

Loaded AwesomePlugin plugin
Enabling AwesomePlugin
Disabling AwesomePlugin

很显然,您不想在任何地方都 panic().那只是一个做些错误的占位符.

Obviously you don't want to panic() everywhere. That's just a placeholder for doing something with the error.

这篇关于golang:如何卸载已经加载的"go plugin"1.8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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