Skip to content

xellio/golang-plugin-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

golang plugin example

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy