Skip to content

Instantly share code, notes, and snippets.

@mbonell
Created January 26, 2018 18:16
Show Gist options
  • Save mbonell/7f68ff5b425c8d1c1147db6792af5b6c to your computer and use it in GitHub Desktop.
Save mbonell/7f68ff5b425c8d1c1147db6792af5b6c to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net/rpc"
"github.com/techwo/rpc-server/service"
)
// Calculator stands for the RPC client implementation.
type Calculator struct {
Client *rpc.Client
}
func main() {
// Connecting to the server
client, err := rpc.DialHTTP("tcp", "127.0.0.1:8767")
if err != nil {
log.Fatal("Dialing:", err)
}
c := &Calculator{Client: client}
result, err := c.addition(5.6, 3.1)
if err != nil {
log.Println("Addition error: " + err.Error())
} else {
log.Printf("Addition result: %f", result)
}
result, err = c.subtraction(7.8, 11)
if err != nil {
log.Println("Subtraction error: " + err.Error())
} else {
log.Printf("Subtraction result: %f", result)
}
}
// addition calls the Addition remote method from the calculator service.
func (c *Calculator) addition(a, b float64) (float64, error) {
args := service.Request{A: a, B: b}
var response service.Response
err := c.Client.Call("Calculator.Addition", args, &response)
return response.Result, err
}
// subtraction calls the Subtraction remote method from the calculator service.
func (c *Calculator) subtraction(a, b float64) (float64, error) {
args := service.Request{A: a, B: b}
var response service.Response
err := c.Client.Call("Calculator.Subtraction", args, &response)
return response.Result, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment