go build -o ./plugin.so -buildmode=plugin ./plugin.go
go test ./
#output
hi there!
--- FAIL: TestTypeAssert2 (0.00s)
panic: interface conversion: plugin.Symbol is func() string, not main.PluginFunc [recovered]
... ...
Created
November 30, 2018 14:58
-
-
Save cp-i-pc/ce79f032086684afce73e7e73f179278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| func Plugin() string { | |
| return "hi there!" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "plugin" | |
| "testing" | |
| ) | |
| type PluginFunc func() string | |
| var pluginSymbol plugin.Symbol | |
| func TestTypeAssert1(t *testing.T) { | |
| f := pluginSymbol.(func() string) | |
| fmt.Println(PluginFunc(f)()) | |
| } | |
| func TestTypeAssert2(t *testing.T) { | |
| f := pluginSymbol.(PluginFunc) | |
| fmt.Println(f()) | |
| } | |
| func init() { | |
| p, _ := plugin.Open("plugin.so") | |
| pluginSymbol, _ = p.Lookup("Plugin") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment