hashicorp / go-plugin

Golang plugin system over RPC.
Mozilla Public License 2.0
5.25k stars 450 forks source link

Passing struct parameter #178

Open aiqency opened 3 years ago

aiqency commented 3 years ago

How am I suppose to pass a struct argument to the basic rpc example?

So far I did that and got the panic shown in the comment:

type Arg struct {
    Name string
}

type Greeter interface {
    Greet(*Arg) string
}

type GreeterRPC struct{ client *rpc.Client }

func (g *GreeterRPC) Greet(args *Arg) string {
    var resp string
    err := g.client.Call("Plugin.Greet", args, &resp)
    if err != nil {

                // panic: gob: local interface type *interface {} can only be decoded 
                // from remote interface type; received concrete type Arg
        panic(err) 

    }

    return resp
}

I'm not very familiar with rpc but the net/rpc package tests works and are pretty straightforward.

args = &Args{7, 8}
reply = new(Reply)
err = client.Call("Arith.Mul", args, reply)

Am I missing something?

Spazzy757 commented 6 months ago

Im getting this as well in the future of 2024 :+1: any chance you found a work around?

2020-MaFte commented 5 months ago

Try change the argument type of GreeterRPCServer.Greet from interface{} to your defined type Arg

//func (s *GreeterRPCServer) Greet(args interface{}, resp *string) error {
func (s *GreeterRPCServer) Greet(args Arg, resp *string) error {
    //*resp = s.Impl.Greet()
        *resp = s.Impl.Greet(args)
    return nil
}