github.com/kaiya/goutils@v1.0.1-0.20230226104005-4ae4a4dc3688/tinyrpc/client/client.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net/rpc"
     6  	"strconv"
     7  
     8  	"github.com/kaiya/goutils/tinyrpc/core"
     9  )
    10  
    11  var (
    12  	Port     = 3344
    13  	addr     = "127.0.0.1:" + strconv.Itoa(Port)
    14  	request  = &core.Request{Name: "world"}
    15  	response = new(core.Response)
    16  )
    17  
    18  func main() {
    19  
    20  	// Establish the connection to the adddress of the
    21  	// RPC server
    22  	client, _ := rpc.Dial("tcp", addr)
    23  	defer client.Close()
    24  
    25  	// Perform a procedure call (core.HandlerName == Handler.Execute)
    26  	// with the Request as specified and a pointer to a response
    27  	// to have our response back.
    28  	err := client.Call(core.HandlerName, request, response)
    29  	if err != nil {
    30  		fmt.Println(err)
    31  		return
    32  	}
    33  	fmt.Println(response.Message)
    34  }