Go Quick Start

This guide gets you started with gRPC in Go with a simple working example.

Prerequisites

  • Go version 1.6 or higher.

For installation instructions, see Go’sGetting Started guide.

gRPC

Use the following command to install gRPC.

  1. $ go get -u google.golang.org/grpc

Protocol Buffers v3

Install the protoc compiler that is used to generate gRPC service code. The simplest way to do this is to download pre-compiled binaries for your platform(protoc-<version>-<platform>.zip) from here:https://github.com/google/protobuf/releases

  • Unzip this file.
  • Update the environment variable PATH to include the path to the protoc binary file.

Next, install the protoc plugin for Go

  1. $ go get -u github.com/golang/protobuf/protoc-gen-go

The compiler plugin, protoc-gen-go, will be installed in $GOBIN, defaultingto $GOPATH/bin. It must be in your PATH for the protocol compiler, protoc,to find it.

  1. $ export PATH=$PATH:$GOPATH/bin

Download the example

The grpc code that was fetched with go get google.golang.org/grpc also contains the examples. They can be found under the examples dir: $GOPATH/src/google.golang.org/grpc/examples.

Build the example

Change to the example directory

  1. $ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld

gRPC services are defined in a .proto file, which is used to generate acorresponding .pb.go file. The .pb.go file is generated by compiling the.proto file using the protocol compiler: protoc.

For the purpose of this example, the helloworld.pb.go file has already beengenerated (by compiling helloworld.proto), and can be found in this directory:$GOPATH/src/google.golang.org/grpc/examples/helloworld/helloworld

This helloworld.pb.go file contains:

  • Generated client and server code.
  • Code for populating, serializing, and retrieving our HelloRequest andHelloReply message types.

Try it!

To compile and run the server and client code, the go run command can be used.In the examples directory:

  1. $ go run greeter_server/main.go

From a different terminal:

  1. $ go run greeter_client/main.go

If things go smoothly, you will see the Greeting: Hello world in the client side output.

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on theserver for the client to call. Our gRPC service is defined using protocolbuffers; you can find out lots more about how to define a service in a .protofile inWhat is gRPC? andgRPC Basics:Go. For now all you need to know is that both the server and the client“stub” have a SayHello RPC method that takes a HelloRequest parameter fromthe client and returns a HelloReply from the server, and that this methodis defined like this:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. }
  6. // The request message containing the user's name.
  7. message HelloRequest {
  8. string name = 1;
  9. }
  10. // The response message containing the greetings
  11. message HelloReply {
  12. string message = 1;
  13. }

Let’s update this so that the Greeter service has two methods. Make sure youare in the same examples dir as above($GOPATH/src/google.golang.org/grpc/examples/helloworld).

Edit helloworld/helloworld.proto and update it with a new SayHelloAgainmethod, with the same request and response types:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. // Sends another greeting
  6. rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
  7. }
  8. // The request message containing the user's name.
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

Generate gRPC code

Next we need to update the gRPC code used by our application to use the newservice definition. From the same examples dir as above($GOPATH/src/google.golang.org/grpc/examples/helloworld):

  1. $ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

This regenerates the helloworld.pb.go with our new changes.

Update and run the application

We now have new generated server and client code, but we still need to implementand call the new method in the human-written parts of our example application.

Update the server

Edit greeter_server/main.go and add the following function to it:

  1. func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
  2. return &pb.HelloReply{Message: "Hello again " + in.GetName()}, nil
  3. }

Update the client

Edit greeter_client/main.go to add the following code to the main function.

  1. r, err = c.SayHelloAgain(ctx, &pb.HelloRequest{Name: name})
  2. if err != nil {
  3. log.Fatalf("could not greet: %v", err)
  4. }
  5. log.Printf("Greeting: %s", r.GetMessage())

Run!

  • Run the server:
  1. $ go run greeter_server/main.go
  • On a different terminal, run the client:
  1. $ go run greeter_client/main.go

You’ll see the following output:

  1. Greeting: Hello world
  2. Greeting: Hello again world

What’s next