Created
May 24, 2016 19:14
-
-
Save ParthDesai/f81afbc9c0490a3edcab58abc2d215f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/rpc" | |
"shared" //Path to the package contains shared struct | |
) | |
type Arith struct { | |
client *rpc.Client | |
} | |
func (t *Arith) Divide(a, b int) shared.Quotient { | |
args := &shared.Args{a, b} | |
var reply shared.Quotient | |
err := t.client.Call("Arithmetic.Divide", args, &reply) | |
if err != nil { | |
log.Fatal("arith error:", err) | |
} | |
return reply | |
} | |
func (t *Arith) Multiply(a, b int) int { | |
args := &shared.Args{a, b} | |
var reply int | |
err := t.client.Call("Arithmetic.Multiply", args, &reply) | |
if err != nil { | |
log.Fatal("arith error:", err) | |
} | |
return reply | |
} | |
func main() { | |
// Tries to connect to localhost:1234 using HTTP protocol (The port on which rpc server is listening) | |
client, err := rpc.DialHTTP("tcp", "localhost:1234") | |
if err != nil { | |
log.Fatal("dialing:", err) | |
} | |
// Create a struct, that mimics all methods provided by interface. | |
// It is not compulsory, we are doing it here, just to simulate a traditional method call. | |
arith := &Arith{client: client} | |
fmt.Println(arith.Multiply(5, 6)) | |
fmt.Println(arith.Divide(500, 10)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment