Demo to showcase Go 1.8 plugins
Download the repo or use go get github.com/chinmay185/golang-plugins-demo to get the code locally.
- Go 1.8 or above
- Linux (for now)
- Glide
Go to simple_example for a demo of calling a function exported from a plugin.
To run the plugins execute the following commands
go build -buildmode=plugin -o plugins/cpu.so plugins/cpu.go
go run main.go
This will execute cpu plugin and display some cpu stats on the console. To display the disk stats (without restarting the main program), execute following commands
go build -buildmode=plugin -o plugins/disk.so plugins/disk.go
curl -X PUT http://localhost:8000/plugins/reload
You should now be able to see disk stats along with cpu stats.
pluginsdirectory contains two plugins, namelycpu.goanddisk.gomain.godefines aMetricPlugininterface- Files in the
pluginsdirectory should export a struct) that satisfies theMetricPlugininterface - Exported variable name follows Title case convention. ex.
cpu.goexportsCpuanddisk.goexportsDisk. If you write a newmem.go, be sure to exportMemfrom that file. main.gofinds all the plugins (i.e. all.sofiles underpluginsdirectory) and execute them. It also starts a http web server which can be used to reload new plugins.- For reloading the new plugins, add the corresponding
.gofiles inpluginsdirectory and build them usinggo build -buildmode=plugin -o plugin.so plugin.gocommand and then use the curl command mentioned above to reload all plugins
- Plugins are only supported on linux (for now)
- Plugin code needs to be compiled with the same version of Go that was used for compiling the main app (for ex. Go 1.9 plugins won't work on Go 1.8 app)
- Plugin has to be in the main package. Packages not named main are ignored. For more info, run
go help buildmode - Plugins can't be reloaded (no hot-code swapping). Refer this issue for more details
- Loading a copy of the already loaded plugin panics. Refer here
MIT