rum
rum

Reputation: 69

Plugin programming on golang

I am coding my project with golang. I want to design my project like plugin programming but I have confused with golang. My project have data analysis task, I purpose make a folder that contain modules analysis, if I have new modules after, I just need copy it to folder and main application will pass data to the new modules without modified. Can you help me? Thanks for watching!

Upvotes: 2

Views: 1849

Answers (2)

I159
I159

Reputation: 31189

Go with it's interfaces is a good choice to build something pluggable.

Interfaces

Interface in Go is a way of types abstraction.

Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here.

It means a simple but very powerful thing - you can use different composite literals as an interface type if they implement the interface. This is already a pluggable system and if are looking for something simple it is possible to build with minimal effort.

Simplest implementation

Let's say you have such a primitive architecture:

▾ pluggable/       
  ▾ app/           
      pusher.go    
  ▾ plugins/       
      green_button.go   
      plugin_interface.go
      red_button.go   
    main.go        

plugins/plugin_interface.go

An interface which will be used as a type abstraction for plugins.

package plugins        

type Button interface {
    Push()             
}                     

plugins/green_button.go

Now it is possible to extend your application with plugins implement the interface.

package plugins              

import (                     
    "fmt"                    
)                            

type GreenButton struct {    
    Msg string               
}                            

func (b GreenButton) Push() {
    fmt.Println(b.Msg)       
}                            

plugins/red_button.go

One more plugin...

package plugins            

import (                   
    "fmt"                  
)                          

type RedButton struct {    
    Err error              
}                          

func (b RedButton) Push() {
    fmt.Println(b.Err)     
}                          

app/pusher.go

An interface type embedded in the composite literal. Any cl implementing the interface could be used in the Pusher instance. Pusher doesn't care about particular plugin implementation it just pushes. It is well abstracted and encapsulated.

package app                            

import (                               
    "github.com/I159/pluggable/plugins"
)                                      

type Pusher struct {                   
    plugins.Button                     
}                                      

main.go

Usage of all the stuff.

package main                                                                

import (                                                                    
    "errors"                                                                
    "github.com/I159/pluggable/app"                                         
    "github.com/I159/pluggable/plugins"                                     
)                                                                           

func main() {                                                               
    alert_pusher := app.Pusher{plugins.RedButton{Err: errors.New("Alert!")}}
    success_pusher := app.Pusher{plugins.GreenButton{Msg: "Well done!"}}    

    alert_pusher.Push()                                                     
    success_pusher.Push()                                                   
}                                                                           

You can add more sugar, for example one more level of isolation to use a single button configured to be a one or another particular implementation and so on.

Plugins as libraries

Same trick with plugin-libraries. A composite literals declared in a plugin-libraries must implement a plugin interface of the main application. But in this case you will need a register a function and a file with libs imports, so it already looks like a nano plugin framework.

Upvotes: 2

Caleb
Caleb

Reputation: 9468

Go 1.8 has support for plugins: https://beta.golang.org/pkg/plugin/. If you can wait a couple months you could use that (or just use the beta 1.8).

Upvotes: 1

Related Questions