The method must look schematically like
func (t *T) MethodName(argType T1, replyType *T2) error
where the T1
and T2
can be marshaled by encoding/gob
. Notice that these requirements apply even if a different codec is used.
The method’s first argument represents the argument provided by the caller, and the second argument represents the result parameters to be returned parameters to be returned to the caller.
package server
import "errors"
type Args struct {
int
A, B
}
type Quotient struct {
int
Que, Rem
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.Breturn nil
}
func (t *Arith) Divide(args *Args, quo *Qutient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.Breturn nil
}
The server calls:
new(Arith)
arith :=
rpc.Register(arith)
rpc.HandleHTTP()"tcp", ":1234")
l, e := net.Listen(if e != nil {
"listen error:", e)
log.Fatal(
}go http.Serve(l, nil)
At this point, clients can see a service “Arith” with methods “Arith.Multiply” and “Arith.Divide”. To invoke one, a client first dials the server:
client,