Skip to content

Commit bdb9561

Browse files
author
Mateusz Gajewski
committed
Example
1 parent 778b522 commit bdb9561

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,43 @@
55

66
**go-bind-plugin** is `go:generate` tool for building [golang 1.8 plugins](https://tip.golang.org/pkg/plugin) and generating wrappers around exported symbols (functions and variables).
77

8+
## Purpose / how it works
9+
10+
**go-bind-plugin** generates neat API around symbols exported by a plugin built with `go build -buildmode=plugin` in upcoming go 1.8. [plugin.Plugin](https://tip.golang.org/pkg/plugin/#Plugin) holds information about exported symbols as map[string]interface{}. go-bind-plugins uses reflection to find out actual types of symbols and generates typed API for a provided plugin with additional functionalities (like dereferencing exported variables and checking sha256 sum). Basic usage does not require plugin sources as wrapper can be generated using only `.so` file.
11+
12+
For example if plugin exports `AddTwoInts(a, b int) int` function instead of using [Plugin.Lookup](https://tip.golang.org/pkg/plugin/#Plugin.Lookup) directly:
13+
14+
```
15+
plug, err := plugin.Open("plugin.so")
16+
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
symbol, err := plug.Lookup("AddTwoInts")
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
if typed, ok := symbol.(func(int, int) int); ok {
27+
result := typed(10, 20)
28+
}
29+
```
30+
31+
you can just simply do:
32+
33+
```
34+
plug, err := BindPluginAPI("plugin.so") // plug is *BingPluginAPI
35+
36+
if err != nil {
37+
panic(err)
38+
}
39+
40+
result := plug.AddTwoInts(10, 20)
41+
```
42+
43+
`BingPluginAPI()` will ensure that plugin exports `AddTwoInts` functions and its type is `func(int, int) int`.
44+
845
## Usage
946

1047
```

0 commit comments

Comments
 (0)