@@ -3,98 +3,148 @@ title: Go Quick Start
3
3
layout : quickstart
4
4
short : Go
5
5
description : This guide gets you started with gRPC in Go with a simple working example.
6
+ protoc-version : 3.11.4
6
7
---
7
8
8
- ### Prerequisites
9
+ <style type =" text/css " >
10
+ ol ol { list-style-type : lower-alpha !important ; }
11
+ </style >
9
12
10
- - Go version 1.6 or higher.
13
+ ### Prerequisites
11
14
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.
13
17
14
18
#### gRPC
15
19
16
- Use the following command to install gRPC.
20
+ Use the following command to install gRPC as a [ module ] [ ] :
17
21
18
22
``` 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 > }}
20
25
```
21
26
22
- #### Protocol Buffers v3
27
+ #### Protocol Buffers
23
28
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] [ ] .
25
32
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:
28
34
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:
30
38
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
+ ```
34
43
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:
38
46
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
+ ` ` `
42
50
43
- ### Download the example
51
+ 3. Update your environment' s path variable to include the path to the
52
+ `protoc` executable. For example:
44
53
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
+ ```
46
57
47
- ### Build the example
58
+ > **MacOS note**: Using [Homebrew][]? Simply run: `brew install protobuf`.
48
59
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:
50
63
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
+ ```
54
67
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:
58
69
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
+ `` `
62
73
63
- This ` helloworld.pb.go ` file contains:
74
+ ### Copy the example
64
75
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:
68
81
69
- ### Try it!
82
+ ` ` ` sh
83
+ $ export MY_EXAMPLES=" $HOME /examples"
84
+ $ mkdir -p " $MY_EXAMPLES "
85
+ ` ` `
70
86
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:
73
88
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
+ ` ` `
77
93
78
- From a different terminal :
94
+ 3. Ensure that the example files are writable since you ' ll be making changes soon :
79
95
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:
83
101
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
+ ` ` `
85
136
86
137
Congratulations! You' ve just run a client-server application with gRPC.
87
138
88
139
### Update a gRPC service
89
140
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:
98
148
99
149
` ` ` protobuf
100
150
// The greeting service definition.
@@ -114,12 +164,8 @@ message HelloReply {
114
164
}
115
165
```
116
166
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:
123
169
124
170
```protobuf
125
171
// The greeting service definition.
@@ -141,26 +187,33 @@ message HelloReply {
141
187
}
142
188
```
143
189
144
- ### Generate gRPC code
190
+ Remember to save the file!
145
191
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:
149
198
150
199
``` sh
151
200
$ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
152
201
```
153
202
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.
155
208
156
209
### Update and run the application
157
210
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.
160
213
161
214
#### Update the server
162
215
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:
164
217
165
218
``` go
166
219
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
170
223
171
224
#### Update the client
172
225
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:
174
228
175
229
``` go
176
230
r, err = c.SayHelloAgain (ctx, &pb.HelloRequest {Name: name})
@@ -180,25 +234,31 @@ if err != nil {
180
234
log.Printf (" Greeting: %s " , r.GetMessage ())
181
235
```
182
236
237
+ Remember to save your changes.
238
+
183
239
#### Run!
184
240
241
+ Run the client and server like you did before. Execute the following commands
242
+ from the ` examples/helloworld ` directory:
243
+
185
244
1 . Run the server:
186
245
187
246
``` sh
188
247
$ go run greeter_server/main.go
189
248
```
190
249
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:
192
252
193
253
` ` ` sh
194
- $ go run greeter_client/main.go
254
+ $ go run greeter_client/main.go Alice
195
255
` ` `
196
256
197
257
You' ll see the following output:
198
258
199
259
```sh
200
- Greeting: Hello world
201
- Greeting: Hello again world
260
+ Greeting: Hello Alice
261
+ Greeting: Hello again Alice
202
262
```
203
263
204
264
### What' s next
@@ -208,3 +268,12 @@ log.Printf("Greeting: %s", r.GetMessage())
208
268
- Work through a more detailed tutorial in [gRPC Basics: Go](/docs/tutorials/basic/go/).
209
269
- Explore the gRPC Go core API in its [reference
210
270
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