An open API service indexing awesome lists of open source software.

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

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...
```