服务端代码

package main

import (
	"log"
	"net"
	"net/rpc"
)

type HelloService struct {}

func (p *HelloService) Hello(request string, reply *string) error  {
	*reply = "hello:" + request
	return nil
}

func main(){
	rpc.RegisterName("HelloService", new(HelloService))
	listener, err := net.Listen("tcp", ":1234")
	if err !=nil{
		log.Fatal("ListenTcp error :", err)
	}
	conn, err := listener.Accept()

	if err != nil {
		log.Fatal("Accept erroe:", err)
	}

	rpc.ServeConn(conn)
}

客户端代码

package main

import (
	"fmt"
	"log"
	"net/rpc"
)

/**
rpc 客户端
 */
func main(){
	client, err := rpc.Dial("tcp", "localhost:1234")
	if err!=nil{
		log.Fatal("dialing:", err)
	}
	var reply string
	err = client.Call("HelloService.Hello", "hello", &reply)
	if err!=nil {
		log.Fatal(err)
	}
	fmt.Print(reply)
}