-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (42 loc) · 887 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"plugin"
"errors"
log "github.com/sirupsen/logrus"
)
// Greeter represents the interface of our plugin
type Greeter interface {
Greet()
}
func main() {
// Load plugin 1
g, err := loadPlugin("./plugin1/plugin.so")
if err != nil {
log.Fatal("Error loading plugin: %v", err)
}
// say hi with plugin 1
g.Greet()
// Load plugin 2
g, err = loadPlugin("./plugin2/plugin.so")
if err != nil {
log.Fatal("Error loading plugin: %v", err)
}
// say hi with plugin 2
g.Greet()
}
// Simple function to open/load the plugin
func loadPlugin(path string) (Greeter, error) {
plug, err := plugin.Open(path)
if err != nil {
return nil, err
}
symGreeter, err := plug.Lookup("Greeter")
if err != nil {
return nil, err
}
greeter, ok := symGreeter.(Greeter)
if !ok {
return nil, errors.New("Unexpected plugin interface")
}
return greeter, nil
}