Skip to content

Commit fe595a4

Browse files
committed
Reworked Go quick start
1 parent e413050 commit fe595a4

File tree

1 file changed

+83
-75
lines changed
  • content/docs/quickstart

1 file changed

+83
-75
lines changed

content/docs/quickstart/go.md

Lines changed: 83 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ description: This guide gets you started with gRPC in Go with a simple working e
77

88
### Prerequisites
99

10-
- Go version 1.6 or higher.
11-
12-
For installation instructions, see Go's [Getting Started](https://golang.org/doc/install) guide.
10+
- **Go** version 1.9+. For installation instructions, see Go's [Getting
11+
Started](https://golang.org/doc/install) guide.
1312

1413
#### gRPC
1514

@@ -19,82 +18,77 @@ Use the following command to install gRPC.
1918
$ go get -u google.golang.org/grpc
2019
```
2120

22-
#### Protocol Buffers v3
23-
24-
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](https://github.com/google/protobuf/releases)
25-
26-
* Unzip this file.
27-
* Update the environment variable `PATH` to include the path to the protoc binary file.
21+
#### Protocol Buffers
2822

29-
Next, install the protoc plugin for Go
30-
31-
```sh
32-
$ go get -u github.com/golang/protobuf/protoc-gen-go
33-
```
23+
While not mandatory, gRPC applications usually leverage [Protocol Buffers][pb]
24+
for service definitions and data serialization, and the example code uses
25+
[proto3][].
3426

35-
The compiler plugin, `protoc-gen-go`, will be installed in `$GOBIN`, defaulting
36-
to `$GOPATH/bin`. It must be in your `PATH` for the protocol compiler, protoc,
37-
to find it.
27+
1. Install the `protoc` compiler:
3828

39-
```sh
40-
$ export PATH=$PATH:$GOPATH/bin
41-
```
29+
- macOS:
4230

43-
### Download the example
31+
```sh
32+
$ brew install protobuf
33+
```
4434

45-
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`.
35+
- Any OS:
4636

47-
### Build the example
37+
1. Download a zip file of the latest version of pre-compiled binaries for
38+
your operating system from [github.com/google/protobuf/releases][]
39+
(`protoc-<version>-<os>.zip`).
40+
2. Unzip the file.
41+
3. Update your environment's path variable to include the path to the
42+
`protoc` executable.
4843
49-
Change to the example directory
44+
2. Install the `protoc` plugin for Go
5045
51-
```sh
52-
$ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
53-
```
46+
```sh
47+
$ go get -u github.com/golang/protobuf/protoc-gen-go
48+
```
5449
55-
gRPC services are defined in a `.proto` file, which is used to generate a
56-
corresponding `.pb.go` file. The `.pb.go` file is generated by compiling the
57-
`.proto` file using the protocol compiler: `protoc`.
50+
3. Update your `PATH` so that the `protoc` compiler can find the plugin:
5851
59-
For the purpose of this example, the `helloworld.pb.go` file has already been
60-
generated (by compiling `helloworld.proto`), and can be found in this directory:
61-
`$GOPATH/src/google.golang.org/grpc/examples/helloworld/helloworld`
52+
```sh
53+
$ export PATH="$PATH:$(go env GOPATH)/bin"
54+
```
6255
63-
This `helloworld.pb.go` file contains:
56+
### Run the example
6457
65-
- Generated client and server code.
66-
- Code for populating, serializing, and retrieving our `HelloRequest` and
67-
`HelloReply` message types.
58+
The grpc sources that you fetched in a previous step also contain the example
59+
code.
6860
69-
### Try it!
61+
1. Change to the example's directory:
7062

71-
To compile and run the server and client code, the `go run` command can be used.
72-
In the examples directory:
63+
```sh
64+
$ cd $(go env GOPATH)/src/google.golang.org/grpc/examples/helloworld
65+
```
7366

74-
```sh
75-
$ go run greeter_server/main.go
76-
```
67+
2. Compile and execute the server code:
7768

78-
From a different terminal:
69+
```sh
70+
$ go run greeter_server/main.go
71+
```
7972

80-
```sh
81-
$ go run greeter_client/main.go
82-
```
73+
3. From a different terminal, compile and execute the client code to see the
74+
client output:
8375

84-
If things go smoothly, you will see the `Greeting: Hello world` in the client side output.
76+
```sh
77+
$ go run greeter_client/main.go
78+
Greeting: Hello world
79+
```
8580

8681
Congratulations! You've just run a client-server application with gRPC.
8782
8883
### Update a gRPC service
8984
90-
Now let's look at how to update the application with an extra method on the
91-
server for the client to call. Our gRPC service is defined using protocol
92-
buffers; you can find out lots more about how to define a service in a `.proto`
93-
file in [What is gRPC?](/docs/guides) and [gRPC Basics:
94-
Go](/docs/tutorials/basic/go/). For now all you need to know is that both the server and the client
95-
"stub" have a `SayHello` RPC method that takes a `HelloRequest` parameter from
96-
the client and returns a `HelloReply` from the server, and that this method
97-
is defined like this:
85+
In this section you'll update the application with an extra server method. The
86+
gRPC service is defined using [protocol buffers][pb]. To learn more about how to
87+
define a service in a `.proto` file see [gRPC Basics:
88+
Go](/docs/tutorials/basic/go). For now, all you need to know is that both the
89+
server and the client stub have a `SayHello()` RPC method that takes a
90+
`HelloRequest` parameter from the client and returns a `HelloReply` from the
91+
server, and that the method is defined like this:
9892
9993
```protobuf
10094
// The greeting service definition.
@@ -114,12 +108,8 @@ message HelloReply {
114108
}
115109
```
116110
117-
Let's update this so that the `Greeter` service has two methods. Make sure you
118-
are in the same examples dir as above
119-
(`$GOPATH/src/google.golang.org/grpc/examples/helloworld`).
120-
121-
Edit `helloworld/helloworld.proto` and update it with a new `SayHelloAgain`
122-
method, with the same request and response types:
111+
Edit `helloworld/helloworld.proto` and add a new `SayHelloAgain()` method, with
112+
the same request and response types:
123113
124114
```protobuf
125115
// The greeting service definition.
@@ -141,26 +131,33 @@ message HelloReply {
141131
}
142132
```
143133

144-
### Generate gRPC code
134+
Remember to save the file!
145135

146-
Next we need to update the gRPC code used by our application to use the new
147-
service definition. From the same examples dir as above
148-
(`$GOPATH/src/google.golang.org/grpc/examples/helloworld`):
136+
### Regenerate gRPC code
137+
138+
Before you can use the new service method, you need to recompile the updated
139+
proto file.
140+
141+
From the `examples/helloworld` directory, run the following command:
149142

150143
```sh
151144
$ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
152145
```
153146

154-
This regenerates the helloworld.pb.go with our new changes.
147+
This will regenerate the `helloworld/helloworld.pb.go` file, which contains:
148+
149+
- Code for populating, serializing, and retrieving `HelloRequest` and
150+
`HelloReply` message types.
151+
- Generated client and server code.
155152

156153
### Update and run the application
157154

158-
We now have new generated server and client code, but we still need to implement
159-
and call the new method in the human-written parts of our example application.
155+
You have regenerated server and client code, but you still need to implement
156+
and call the new method in the human-written parts of the example application.
160157

161158
#### Update the server
162159

163-
Edit `greeter_server/main.go` and add the following function to it:
160+
Open `greeter_server/main.go` and add the following function to it:
164161

165162
```go
166163
func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
@@ -170,7 +167,8 @@ func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.He
170167

171168
#### Update the client
172169

173-
Edit `greeter_client/main.go` to add the following code to the main function.
170+
Open `greeter_client/main.go` to add the following code to the end of the
171+
`main()` function body:
174172

175173
```go
176174
r, err = c.SayHelloAgain(ctx, &pb.HelloRequest{Name: name})
@@ -180,25 +178,31 @@ if err != nil {
180178
log.Printf("Greeting: %s", r.GetMessage())
181179
```
182180

181+
Remember to save your changes.
182+
183183
#### Run!
184184

185+
Run the client and server like you did before. Execute the following commands
186+
from the `examples/helloworld` directory:
187+
185188
1. Run the server:
186189

187190
```sh
188191
$ go run greeter_server/main.go
189192
```
190193

191-
2. On a different terminal, run the client:
194+
2. From another terminal, run the client. This time, add a name as a
195+
command-line argument:
192196

193197
```sh
194-
$ go run greeter_client/main.go
198+
$ go run greeter_client/main.go Alice
195199
```
196200

197201
You'll see the following output:
198202
199203
```sh
200-
Greeting: Hello world
201-
Greeting: Hello again world
204+
Greeting: Hello Alice
205+
Greeting: Hello again Alice
202206
```
203207
204208
### What's next
@@ -208,3 +212,7 @@ log.Printf("Greeting: %s", r.GetMessage())
208212
- Work through a more detailed tutorial in [gRPC Basics: Go](/docs/tutorials/basic/go/).
209213
- Explore the gRPC Go core API in its [reference
210214
documentation](https://godoc.org/google.golang.org/grpc).
215+
216+
[github.com/google/protobuf/releases]: https://github.com/google/protobuf/releases
217+
[pb]: https://developers.google.com/protocol-buffers
218+
[proto3]: https://developers.google.com/protocol-buffers/docs/proto3

0 commit comments

Comments
 (0)