Go

Plugin

Introduction#

Go provides a plugin mechanism that can be used to dynamically link other Go code at runtime.

As of Go 1.8, it is only usable on Linux.

Defining and using a plugin

package main

import "fmt"

var V int

func F() { fmt.Printf("Hello, number %d\n", V) }

This can be built with:

go build -buildmode=plugin

And then loaded and used from your application:

p, err := plugin.Open("plugin_name.so")
if err != nil {
    panic(err)
}

v, err := p.Lookup("V")
if err != nil {
    panic(err)
}

f, err := p.Lookup("F")
if err != nil {
    panic(err)
}

*v.(*int) = 7
f.(func())() // prints "Hello, number 7"

Example from The State of Go 2017.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow