type MathService int func (m *MathService) Add(args *[]int, result *int) error { sum := 0 for _, arg := range *args { sum += arg } *result = sum return nil } func main() { math := new(MathService) server := rpc.NewServer() server.RegisterName("Math", math) listener, _ := net.Listen("tcp", ":8080") server.Accept(listener) }
type HelloService struct{} func (h *HelloService) SayHello(name string, message *string) error { *message = "Hello, " + name + "!" return nil } func main() { hello := new(HelloService) server := rpc.NewServer() server.RegisterName("Hello", hello) transport, _ := net.Dial("tcp", "localhost:8080") client := rpc.NewClient(transport) var response string client.Call("Hello.SayHello", "John", &response) fmt.Println(response) }In this example, a HelloService type is defined which has a SayHello method that takes a name string as argument and returns a greeting message. The RegisterName method is used to register the HelloService object with a name "Hello" on the server, and a client is initiated to make a remote procedure call to the server's SayHello method with the name "John". The returned response is printed on the console. The package library used in the examples is "net/rpc".