Golang net/rpc.Client.Go() function example

package net/rpc

Golang net/rpc.Client.Go() function usage example

 type Args struct {
  A, B int
 }

 type Reply struct {
  C int
 }

 args := Args{8, 8}

 mulReply := new(Reply)

 mulCall = client.Go("Operator.Multiply", args, mulReply, nil)

 addReply := new(Reply)

 addCall = client.Go("Operator.Addition", args, addReply, nil)

 addCall = <-addCall.Done

 if addCall.Error != nil {
  fmt.Printf("Addition : expected no error but got string %q", addCall.Error.Error())
 }

 if addReply.C != args.A+args.B {
  fmt.Printf("Addition : got %d expected %d", addReply.C, args.A+args.B)
 }

 mulCall = <-mulCall.Done

 if mulCall.Error != nil {
  fmt.Printf("Multiply : expected no error but got string %q", mulCall.Error.Error())
 }
 if mulReply.C != args.A*args.B {
  fmt.Printf("Multiply : got %d expected %d", mulReply.C, args.A*args.B)
 }

References :

http://golang.org/pkg/net/rpc/#Client.Go

http://golang.org/src/net/rpc/jsonrpc/all_test.go

Advertisement