summaryrefslogtreecommitdiff
path: root/cmds/gocli-as-plugin/main.go
blob: a9bd632e8925353fd3ad57d77c9ebe218c2116e0 (plain)
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
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"log"
	"os"

	"plugin"
//	"github.com/awesome-gocui/gocui"
)

type Greeter interface {
	Greet()
}

var plugGocli *plugin.Plugin
var plugHello *plugin.Plugin

func main() {
	log.Println("attempt plugin")

	go loadPlugin(plugHello, "../../toolkit/hello.so")
	loadPlugin(plugGocli, "../../toolkit/gocli.so")
}

func loadPlugin(plug *plugin.Plugin, name string) {
	// load module
	// 1. open the so file to load the symbols
	plug, err = plugin.Open(name)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	// 2. look up a symbol (an exported function or variable)
	// in this case, variable Greeter
	symGreeter, err := plug.Lookup("Greeter")
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	log.Println("symGreater", symGreeter)

	// 3. Assert that loaded symbol is of a desired type
	// in this case interface type Greeter (defined above)
	// var greeter Greeter
	greeter, ok := symGreeter.(Greeter)
	if !ok {
		log.Println("unexpected type from module symbol")
		os.Exit(1)
	}

	// 4. use the module
	greeter.Greet()
}