-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
85 lines (71 loc) · 1.5 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"os"
"plugin"
"shared"
)
type Base struct {
name string
}
func (b Base) Name() string {
return b.name
}
func (b Base) ID() (string, bool) {
return "", false
}
type Extended struct {
Base
id string
}
func (e Extended) Name() string {
return e.name
}
func (e Extended) ID() (string, bool) {
return e.id, true
}
func main() {
// load module
// 1. open the so file to load the symbols
plug, err := plugin.Open("./plugins/plugin.so")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 2. look up a symbol (an exported function or variable)
// in this case, variable Greeter
symVersion, err := plug.Lookup("Version")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ver, ok := symVersion.(*string)
if !ok {
fmt.Println("unexpected type from module symbol")
fmt.Printf("%T", symVersion)
os.Exit(1)
}
fmt.Println("PluginVersion: " + *ver)
// 3. look up a symbol (an exported function or variable)
// in this case, variable Greeter
symPlugin, err := plug.Lookup("PluginSymbol")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 4. Assert that loaded symbol is of a desired type
// in this case interface type Greeter (defined above)
p, ok := symPlugin.(shared.Plugin_v1)
if !ok {
fmt.Println("unexpected type from module symbol")
fmt.Printf("%T", symVersion)
os.Exit(1)
}
data := Extended{}
data.name = "JohnDoe"
data.id = "unknown"
// 5. use the module with base access
p.Init(data.Base)
// 6. use the module with extended access
p.Init(data)
}