package main import ( "log" "net" "net/rpc" ) type MyService int func (s *MyService) Sum(args []int, reply *int) error { sum := 0 for _, num := range args { sum += num } *reply = sum return nil } func main() { service := new(MyService) rpc.Register(service) listener, err := net.Listen("tcp", ":8080") if err != nil { log.Fatalf("Failed to listen: %v", err) } for { conn, err := listener.Accept() if err != nil { log.Printf("Failed to accept client connection: %v", err) continue } go rpc.ServeConn(conn) } }
package main import ( "log" "net/rpc" ) func main() { client, err := rpc.Dial("tcp", "localhost:8080") if err != nil { log.Fatalf("Failed to dial server: %v", err) } args := []int{1, 2, 3, 4, 5} var reply int err = client.Call("MyService.Sum", args, &reply) if err != nil { log.Fatalf("Failed to call remote method: %v", err) } log.Printf("Result: %d", reply) }In this case, the `rpc.Client` is used to establish a connection with the server, and then `Client.Call()` is used to invoke the remote method `MyService.Sum()` with its arguments. The result of the method call is stored in the `reply` variable. The `net/rpc` package is part of the standard library of Go.