douzhuan1467 2017-04-12 16:43
浏览 1829

golang:如何卸载已经加载的“ go plugin” 1.8

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

but unload plugin is not supported.

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?

  • 写回答

1条回答 默认 最新

  • dpxbc88022 2017-04-12 22:36
    关注

    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
    

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

    评论

报告相同问题?

悬赏问题

  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow