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)
Go to simple_example for a demo of calling a function exported from a plugin.
Running CPU and Disk Metrics 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.
CPU and Disk Metrics plugin
pluginsdirectory contains two plugins, namely
- Files in the
pluginsdirectory should export a struct) that satisfies the
- Exported variable name follows Title case convention. ex.
Disk. If you write a new
mem.go, be sure to export
Memfrom that file.
main.gofinds all the plugins (i.e. all
pluginsdirectory) 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
pluginsdirectory and build them using
go build -buildmode=plugin -o plugin.so plugin.gocommand and then use the curl command mentioned above to reload all plugins
Some Go plugin limitations / gotchas
- 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