6 "github.com/hashicorp/go-plugin"
9 // Greeter is the interface that we're exposing as a plugin.
10 type Greeter interface {
14 // Here is an implementation that talks over RPC
15 type GreeterRPC struct{ client *rpc.Client }
17 func (g *GreeterRPC) Greet() string {
19 err := g.client.Call("Plugin.Greet", new(interface{}), &resp)
21 // You usually want your interfaces to return errors. If they don't,
22 // there isn't much other choice here.
29 // Here is the RPC server that GreeterRPC talks to, conforming to
30 // the requirements of net/rpc
31 type GreeterRPCServer struct {
32 // This is the real implementation
36 func (s *GreeterRPCServer) Greet(args interface{}, resp *string) error {
37 *resp = s.Impl.Greet()
41 // This is the implementation of plugin.Plugin so we can serve/consume this
43 // This has two methods: Server must return an RPC server for this plugin
44 // type. We construct a GreeterRPCServer for this.
46 // Client must return an implementation of our interface that communicates
47 // over an RPC client. We return GreeterRPC for this.
49 // Ignore MuxBroker. That is used to create more multiplexed streams on our
50 // plugin connection and is a more advanced use case.
51 type GreeterPlugin struct {
56 func (p *GreeterPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
57 return &GreeterRPCServer{Impl: p.Impl}, nil
60 func (GreeterPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
61 return &GreeterRPC{client: c}, nil