Archive for January, 2020

 

Building and using a plugin in Go / Golang

Jan 15, 2020 in GoLang

From https://medium.com/quick-code/write-a-web-service-with-go-plug-ins-c0472e0645e6

The plugin module (pluginex1.go):


package main

import "fmt"

func Talk() {
    fmt.Println("Hello From Plugin !")
}

Build the plugin module:


# go build -buildmode=plugin -o pluginex1.so pluginex1.go

Invoke the plugin module (pluginex1main.go):


package main

import(
        "os"
        "fmt"
        "plugin"
)

func main() {
        plugin, err := plugin.Open("pluginex1.so")
        if err != nil {
                fmt.Println(err)
                os.Exit(-1)
        }
        sym, err := plugin.Lookup("Talk") // reflection
        if err != nil {
                fmt.Println(err)
                os.Exit(-1)
        }
        sym.(func())()
}

. . .