trpc.group/trpc-go/trpc-go@v1.0.3/docs/quick_start.md (about) 1 English | [中文](quick_start.zh_CN.md) 2 3 ## Quick Start 4 5 ### Prerequisites 6 7 - **[Go](https://go.dev/doc/install)**, should be greater or equal than go1.18. 8 - **[tRPC cmdline tools](https://github.com/trpc-group/trpc-cmdline)**, to generate stub codes from protobuf. 9 10 ### Get Example Code 11 12 Example code is part of tRPC-Go repo. 13 Clone and change directory to helloworld. 14 ```bash 15 $ git clone --depth 1 git@github.com:trpc-group/trpc-go.git 16 $ cd trpc-go/examples/helloworld 17 ``` 18 19 ### Run the Example 20 21 1. Compile and execute the server code: 22 ```bash 23 $ cd server && go run main.go 24 ``` 25 2. From a different terminal, compile and execute the client code: 26 ```bash 27 $ cd client && go run main.go 28 ``` 29 You will see `Hello world!` displayed as a log. 30 31 Congratulations! You’ve just run a client-server application with tRPC-Go. 32 33 ### Update protobuf 34 35 As you can see, service `Greeter` are defined in protobuf `./pb/helloworld.proto` as following: 36 ```protobuf 37 service Greeter { 38 rpc Hello (HelloRequest) returns (HelloReply) {} 39 } 40 41 message HelloRequest { 42 string msg = 1; 43 } 44 45 message HelloReply { 46 string msg = 1; 47 } 48 ``` 49 It has only one method `Hello`, which takes `HelloRequest` as parameter and returns `HelloReply`. 50 51 Now, add a new method `HelloAgain`, with the same request and response: 52 ```protobuf 53 service Greeter { 54 rpc Hello (HelloRequest) returns (HelloReply) {} 55 rpc HelloAgain (HelloRequest) returns (HelloReply) {} 56 } 57 58 59 message HelloRequest { 60 string msg = 1; 61 } 62 63 message HelloReply { 64 string msg = 1; 65 } 66 ``` 67 68 Regenerate tRPC code by `$ make` in `./pb` directory. 69 The Makefile calls `trpc` which should be installed by prerequisites. 70 71 ### Update and Run Server and Client 72 73 At server side `server/main.go`, add codes to implement `HelloAgain`: 74 ```go 75 func (g Greeter) HelloAgain(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) { 76 log.Infof("got HelloAgain request: %s", req.Msg) 77 return &pb.HelloReply{Msg: "Hello " + req.Msg + " again!"}, nil 78 } 79 ``` 80 81 At client side `client/main.go`, add codes to call `HelloAgain`: 82 ```go 83 rsp, err = c.HelloAgain(context.Background(), &pb.HelloRequest{Msg: "world"}) 84 if err != nil { 85 log.Error(err) 86 } 87 log.Info(rsp.Msg) 88 ``` 89 90 Follow the `Run the Example` section to re-run your example and you will see `Hello world again!` in client log. 91 92 ### What's Next 93 94 - Learn [tRPC Design](https://github.com/trpc-group/trpc). 95 - Read [basics tutorial](./basics_tutorial.md) to get deeper into tRPC-Go. 96 - Explore the [API reference](https://pkg.go.dev/trpc.group/trpc-go/trpc-go). 97