Trying to implement Go plugin.
Here’s my plugin code
package main
import (
"errors"
)
// Config represents configuration data.
type Config struct {
Path string
}
// Message represents a message.
type Message struct {
EventMessage string
}
// Assessment represents an assessment.
type Assessment struct {
AtRisk bool
}
// RulePlugin represents a plugin that implements the Plugin interface.
type rulePlugin struct {
}
// Init initializes the plugin with the provided configuration.
func (p *rulePlugin) Init(config *Config) error {
if config == nil {
return errors.New("config is nil")
}
// Additional initialization logic can go here if needed
return nil
}
// ProcessMessage processes the message and returns an assessment.
func (p *rulePlugin) ProcessMessage(message *Message) (*Assessment, error) {
if message == nil {
return nil, errors.New("message is nil")
}
// Example assessment logic
atRisk := len(message.EventMessage) > 10
return &Assessment{AtRisk: atRisk}, nil
}
// exported
var Plugin *rulePlugin
Plugin build command:
go build -gcflags=”all=-N -l” -buildmode=plugin -o rulePlugin.so rulePlugin.go
rulePlugin.so built successfully!
Here’s my code to invoke Plugin
//go:build exclude
package main
import (
"fmt"
"plugin"
)
// Plugin defines the interface for a plugin.
type Plugin interface {
Init(config *Config) error
ProcessMessage(message *Message) (*Assessment, error)
}
// Config represents configuration data.
type Config struct {
Path string
}
// Message represents a message.
type Message struct {
EventMessage string
}
// Assessment represents an assessment.
type Assessment struct {
AtRisk bool
}
func main() {
// Load the plugin
p, err := plugin.Open("rulePlugin.so")
if err != nil {
fmt.Println("Error loading plugin:", err)
return
}
// Lookup the symbol representing the Plugin interface
symPlugin, err := p.Lookup("Plugin")
if err != nil {
fmt.Println("Error looking up Plugin symbol:", err)
return
}
// Assert that the symbol implements the Plugin interface
var rulePlugin Plugin
rulePlugin, ok := symPlugin.(Plugin)
if !ok {
fmt.Println("Plugin does not implement Plugin interface")
return
}
// Initialize the plugin with some configuration
config := &Config{Path: "/some/path"}
if err := rulePlugin.Init(config); err != nil {
fmt.Println("Error initializing plugin:", err)
return
}
// Create a sample message
message := &Message{EventMessage: "This is a test message"}
// Process the message using the plugin
assessment, err := rulePlugin.ProcessMessage(message)
if err != nil {
fmt.Println("Error processing message:", err)
return
}
// Print the assessment result
fmt.Println("Assessment result:", assessment.AtRisk)
}
Trying to execute the above code like:
go run rulePlugintest.go
Plugin does not implement Plugin interface
I do not understand whats going on here and what I am missing.
I have checked many samples, for example this one: https://github.com/huantt/golang-plugin-examle/blob/main/main.go which executes great. Not sure what I am missing in my implementation, any ideas would be appreciated.
1