-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (54 loc) · 1.08 KB
/
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
53
54
55
56
57
58
59
60
61
package main
import (
"flag"
"fmt"
"plugin"
"github.com/guesslin/plug/types"
)
func main() {
mode := flag.String("mode", "adder", "load which kind [adder|greet] of plugin")
path := flag.String("path", "", "path to plugin file")
flag.Parse()
if *path == "" {
fmt.Println("No path given")
return
}
fmt.Println("======= main =======")
switch *mode {
case "adder":
adder := loadAdder(*path)
op1 := 100
op2 := 20
fmt.Printf("Adder %d + %d = %d result\n", op1, op2, adder.Add(op1, op2))
case "greet":
loadGreet(*path)
default:
fmt.Printf("mode %s not supported!\n", *mode)
}
}
func loadPlugin(path string) *plugin.Plugin {
p, err := plugin.Open(path)
mustOK(err)
return p
}
func loadAdder(path string) types.Adder {
p := loadPlugin(path)
f, err := p.Lookup("Factory")
mustOK(err)
factory, ok := f.(*types.Factory)
if !ok {
panic("assert failed")
}
return (*factory)()
}
func loadGreet(path string) {
p := loadPlugin(path)
gFunc, err := p.Lookup("Greet")
mustOK(err)
gFunc.(func(n string))("guesslin")
}
func mustOK(err error) {
if err != nil {
panic(err)
}
}