https://github.com/xellio/golang-plugin-example
golang plugin example
https://github.com/xellio/golang-plugin-example
go golang plugin-loader
Last synced: 2 months ago
JSON representation
golang plugin example
- Host: GitHub
- URL: https://github.com/xellio/golang-plugin-example
- Owner: xellio
- License: mit
- Created: 2018-11-16T15:26:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-19T09:33:17.000Z (over 7 years ago)
- Last Synced: 2024-06-19T20:51:49.308Z (almost 2 years ago)
- Topics: go, golang, plugin-loader
- Language: Go
- Size: 4.88 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## GO Plugins example
A small example application for using plugins in go.
Using plugins, it is possible to add/remove functionality to a compiled go program at runtime.
In this example, I expect that a plugin has a function New (see: cmd/cli/main.go +29).
I also expect that the return value of this New function implements a Module interface (see: pkg/wrapper/module.go). In this application, a Module has needs a fuction called Execute().
If you want to create a new plugin for this application, you have to implement those two functions at a minimum.
### Example:
Clone and build the example application:
```
go get -u github.com/xellio/golang-plugin-example
cd $GOPATH/src/github.com/xellio/golang-plugin-example/
make
```
Run the application:
```
./bin/modules
Hello from FIRST...
Hello from SECOND...
Hello from THIRD - Written in C...
Hello from FIRST...
Hello from THIRD - Written in C...
Hello from SECOND...
Hello from FIRST...
Hello from SECOND...
Hello from THIRD - Written in C...
...
```
***NOTE:*** In this example, the plugins were executed in a goroutine - so the order of the output can change.
In the application root ($GOPATH/src/github.com/xellio/golang-plugin-example/) you will find a plugins/ directory, conaining the loaded plugins (.so files):
```
...
├── plugins
│ ├── first_plugin.so
│ ├── second_plugin.so
│ └── third_plugin.so
...
```
Removing one of those plugins **while running the application** will stop executing the removed plugin:
```
mv ./plugins/second_plugin.so .
```
Output:
```
Hello from FIRST...
Hello from THIRD - Written in C...
Hello from SECOND...
Hello from THIRD - Written in C...
Hello from FIRST...
Hello from FIRST...
Hello from THIRD - Written in C...
Hello from THIRD - Written in C...
Hello from FIRST...
```
(Re)adding a plugin to the plugins/ directory will (re)execute the plugin:
```
mv ./second_plugin.so ./plugins/
```
Output:
```
Hello from THIRD - Written in C...
Hello from FIRST...
Hello from FIRST...
Hello from THIRD - Written in C...
Hello from SECOND...
Hello from FIRST...
Hello from THIRD - Written in C...
Hello from SECOND...
```