-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
57 lines (44 loc) · 1.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
}