Skip to content

Commit ea72ee4

Browse files
authored
Reworked Go quick start (#183)
* Reworked Go quick start * [WIP] Use Go v1.12+ and fetch as modules * Add steps to make local copy of example sources * Post-review updates
1 parent f911fc8 commit ea72ee4

File tree

1 file changed

+143
-74
lines changed
  • content/docs/quickstart

1 file changed

+143
-74
lines changed

content/docs/quickstart/go.md

Lines changed: 143 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,148 @@ title: Go Quick Start
33
layout: quickstart
44
short: Go
55
description: This guide gets you started with gRPC in Go with a simple working example.
6+
protoc-version: 3.11.4
67
---
78

8-
### Prerequisites
9+
<style type="text/css">
10+
ol ol { list-style-type: lower-alpha !important; }
11+
</style>
912

10-
- Go version 1.6 or higher.
13+
### Prerequisites
1114

12-
For installation instructions, see Go's [Getting Started](https://golang.org/doc/install) guide.
15+
- **[Go][]**, any one of the **three latest major** [releases of Go][]. For
16+
installation instructions, see Go's [Getting Started][] guide.
1317

1418
#### gRPC
1519

16-
Use the following command to install gRPC.
20+
Use the following command to install gRPC as a [module][]:
1721

1822
```sh
19-
$ go get -u google.golang.org/grpc
23+
$ export GO111MODULE=on # Enable module-aware mode
24+
$ go get google.golang.org/grpc@{{< param grpc_release_tag >}}
2025
```
2126

22-
#### Protocol Buffers v3
27+
#### Protocol Buffers
2328

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)
29+
While not mandatory, gRPC applications usually leverage [Protocol Buffers][pb]
30+
for service definitions and data serialization, and the example code uses
31+
[proto3][].
2532

26-
* Unzip this file.
27-
* Update the environment variable `PATH` to include the path to the protoc binary file.
33+
1. Install the `protoc` compiler:
2834

29-
Next, install the protoc plugin for Go
35+
1. Download a zip file of the latest version of pre-compiled binaries for
36+
your operating system from [github.com/google/protobuf/releases][]
37+
(`protoc-<version>-<os><arch>.zip`). For example:
3038

31-
```sh
32-
$ go get -u github.com/golang/protobuf/protoc-gen-go
33-
```
39+
```sh
40+
$ PB_REL="https://github.com/protocolbuffers/protobuf/releases"
41+
$ curl -LO $PB_REL/download/v{{< param protoc-version >}}/protoc-{{< param protoc-version >}}-linux-x86_64.zip
42+
```
3443

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.
44+
2. Unzip the file under `$HOME/.local` or a directory of your choice. For
45+
example:
3846

39-
```sh
40-
$ export PATH=$PATH:$GOPATH/bin
41-
```
47+
```sh
48+
$ unzip protoc-{{< param protoc-version >}}-linux-x86_64.zip -d $HOME/.local
49+
```
4250

43-
### Download the example
51+
3. Update your environment's path variable to include the path to the
52+
`protoc` executable. For example:
4453
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`.
54+
```sh
55+
$ export PATH="$PATH:$HOME/.local/bin"
56+
```
4657
47-
### Build the example
58+
> **MacOS note**: Using [Homebrew][]? Simply run: `brew install protobuf`.
4859
49-
Change to the example directory
60+
2. The `protoc` plugin for Go (`protoc-gen-go`) was installed as a dependency
61+
of the `grpc` module. You can confirm this, or install the plugin, using the
62+
following command:
5063
51-
```sh
52-
$ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
53-
```
64+
```sh
65+
$ go get github.com/golang/protobuf/protoc-gen-go
66+
```
5467
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`.
68+
3. Update your `PATH` so that the `protoc` compiler can find the plugin:
5869
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`
70+
```sh
71+
$ export PATH="$PATH:$(go env GOPATH)/bin"
72+
```
6273
63-
This `helloworld.pb.go` file contains:
74+
### Copy the example
6475
65-
- Generated client and server code.
66-
- Code for populating, serializing, and retrieving our `HelloRequest` and
67-
`HelloReply` message types.
76+
The example code is part of the `grpc` source, which you fetched by following
77+
steps of the previous section. You'll need a local copy of the example code to
78+
work through this quick start.
79+
80+
1. Choose a suitable working directory, and ensure that it exists:
6881

69-
### Try it!
82+
```sh
83+
$ export MY_EXAMPLES="$HOME/examples"
84+
$ mkdir -p "$MY_EXAMPLES"
85+
```
7086

71-
To compile and run the server and client code, the `go run` command can be used.
72-
In the examples directory:
87+
2. Copy the example source:
7388

74-
```sh
75-
$ go run greeter_server/main.go
76-
```
89+
```sh
90+
$ EX_SRC_DIR="$(go env GOPATH)/pkg/mod/google.golang.org/grpc@{{< param grpc_release_tag >}}/examples"
91+
$ cp -R "$EX_SRC_DIR/helloworld" "$MY_EXAMPLES"
92+
```
7793

78-
From a different terminal:
94+
3. Ensure that the example files are writable since you'll be making changes soon:
7995
80-
```sh
81-
$ go run greeter_client/main.go
82-
```
96+
```sh
97+
$ chmod -R u+w "$MY_EXAMPLES/helloworld"
98+
```
99+
100+
4. Change to the example's directory:
83101

84-
If things go smoothly, you will see the `Greeting: Hello world` in the client side output.
102+
```sh
103+
$ cd "$MY_EXAMPLES/helloworld"
104+
```
105+
106+
5. Setup the example as a module:
107+
108+
```sh
109+
$ go mod init examples/helloworld
110+
```
111+
112+
6. Adjust import paths to reference local packages (rather than those from the
113+
original `google.golang.org/grpc` module):
114+
115+
```sh
116+
$ perl -pi -e 's|google.golang.org/grpc/||g' greeter_{client,server}/main.go
117+
```
118+
119+
### Run the example
120+
121+
From the `$MY_EXAMPLES/helloworld` directory:
122+
123+
1. Compile and execute the server code:
124+
125+
```sh
126+
$ go run greeter_server/main.go
127+
```
128+
129+
2. From a different terminal, compile and execute the client code to see the
130+
client output:
131+
132+
```sh
133+
$ go run greeter_client/main.go
134+
Greeting: Hello world
135+
```
85136

86137
Congratulations! You've just run a client-server application with gRPC.
87138
88139
### Update a gRPC service
89140
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:
141+
In this section you'll update the application with an extra server method. The
142+
gRPC service is defined using [protocol buffers][pb]. To learn more about how to
143+
define a service in a `.proto` file see [gRPC Basics:
144+
Go](/docs/tutorials/basic/go). For now, all you need to know is that both the
145+
server and the client stub have a `SayHello()` RPC method that takes a
146+
`HelloRequest` parameter from the client and returns a `HelloReply` from the
147+
server, and that the method is defined like this:
98148
99149
```protobuf
100150
// The greeting service definition.
@@ -114,12 +164,8 @@ message HelloReply {
114164
}
115165
```
116166
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:
167+
Edit `helloworld/helloworld.proto` and add a new `SayHelloAgain()` method, with
168+
the same request and response types:
123169
124170
```protobuf
125171
// The greeting service definition.
@@ -141,26 +187,33 @@ message HelloReply {
141187
}
142188
```
143189

144-
### Generate gRPC code
190+
Remember to save the file!
145191

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`):
192+
### Regenerate gRPC code
193+
194+
Before you can use the new service method, you need to recompile the updated
195+
proto file.
196+
197+
From the `$MY_EXAMPLES/helloworld` directory, run the following command:
149198

150199
```sh
151200
$ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
152201
```
153202

154-
This regenerates the helloworld.pb.go with our new changes.
203+
This will regenerate the `helloworld/helloworld.pb.go` file, which contains:
204+
205+
- Code for populating, serializing, and retrieving `HelloRequest` and
206+
`HelloReply` message types.
207+
- Generated client and server code.
155208

156209
### Update and run the application
157210

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.
211+
You have regenerated server and client code, but you still need to implement
212+
and call the new method in the human-written parts of the example application.
160213

161214
#### Update the server
162215

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

165218
```go
166219
func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
@@ -170,7 +223,8 @@ func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.He
170223

171224
#### Update the client
172225

173-
Edit `greeter_client/main.go` to add the following code to the main function.
226+
Open `greeter_client/main.go` to add the following code to the end of the
227+
`main()` function body:
174228

175229
```go
176230
r, err = c.SayHelloAgain(ctx, &pb.HelloRequest{Name: name})
@@ -180,25 +234,31 @@ if err != nil {
180234
log.Printf("Greeting: %s", r.GetMessage())
181235
```
182236

237+
Remember to save your changes.
238+
183239
#### Run!
184240

241+
Run the client and server like you did before. Execute the following commands
242+
from the `examples/helloworld` directory:
243+
185244
1. Run the server:
186245

187246
```sh
188247
$ go run greeter_server/main.go
189248
```
190249

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

193253
```sh
194-
$ go run greeter_client/main.go
254+
$ go run greeter_client/main.go Alice
195255
```
196256

197257
You'll see the following output:
198258
199259
```sh
200-
Greeting: Hello world
201-
Greeting: Hello again world
260+
Greeting: Hello Alice
261+
Greeting: Hello again Alice
202262
```
203263
204264
### What's next
@@ -208,3 +268,12 @@ log.Printf("Greeting: %s", r.GetMessage())
208268
- Work through a more detailed tutorial in [gRPC Basics: Go](/docs/tutorials/basic/go/).
209269
- Explore the gRPC Go core API in its [reference
210270
documentation](https://godoc.org/google.golang.org/grpc).
271+
272+
[github.com/google/protobuf/releases]: https://github.com/google/protobuf/releases
273+
[Getting Started]: https://golang.org/doc/install
274+
[Go]: https://golang.org
275+
[Homebrew]: https://brew.sh
276+
[module]: https://github.com/golang/go/wiki/Modules
277+
[pb]: https://developers.google.com/protocol-buffers
278+
[proto3]: https://developers.google.com/protocol-buffers/docs/proto3
279+
[releases of Go]: https://golang.org/doc/devel/release.html

0 commit comments

Comments
 (0)