代码样例¶
这个目录包含了使用协议缓冲区来管理地址簿的示例代码。每种支持的语言都提供了两个程序。add_person 示例将一个新的人添加到地址簿中,提示用户输入该人的信息。list_people 的例子是列出已经在地址簿中的人。这些例子在三种语言中使用完全相同的格式,因此你可以,例如,使用 add_person_java 来创建一个地址簿,然后使用 list_people_python 来读取它。
这些例子是协议缓冲区教程的一部分,位于:https://developers.google.com/protocol-buffers/docs/tutorials
使用 bazel 构建例子¶
这个例子需要 bazel 0.5.4 或更新的版本来构建。你可以从 bazel 的发布页面下载/安装最新版本的 bazel:
https://github.com/bazelbuild/bazel/releases
一旦你安装了 bazel,只需在这个 examples 目录下运行以下命令即可构建代码:
$ bazel build :all
然后你就可以运行建立的二进制文件了:
$ bazel-bin/add_person_cpp addressbook.data
使用 make 构建示例¶
你必须先安装 protobuf 包,然后才能用 make 构建它。最低要求是安装协议编译器(即 protoc 二进制)和你要构建的语言的 protobuf 运行时。
你可以简单地运行 “make” 来构建所有语言的例子(Go 除外)。然而,由于不同的语言有不同的安装要求,它很可能会失败。最好是按照下面的个别说明,只构建你感兴趣的语言。
C++¶
你可以按照 …/src/README.md 中的说明,从源代码中安装 protoc 和 protobuf C++ 运行时。
然后在这个例子目录下运行 “make cpp” 来构建 C++ 例子。它将创建两个可执行文件:add_person_cpp 和 list_people_cpp。这些程序只是接受一个地址簿文件作为参数。add_person_cpp 程序将创建该文件,如果它还不存在的话。
要运行这些例子:
$ ./add_person_cpp addressbook.data
$ ./list_people_cpp addressbook.data
注意,在某些平台上,你可能必须编辑 Makefile,并从链接器命令中删除 “-lpthread” (也许用其他东西代替)。我们没有自动这样做,因为我们想让这个例子保持简单。
Python¶
按照 …/README.md 中的说明安装 protoc,然后按照 …/python/README.md 从源代码安装 protobuf python runtime。你也可以用 pip 安装 python 运行时:
$ pip install protobuf
确保运行时的版本与 protoc 二进制相同,否则可能无法工作。
在你安装了 protoc 和 python 运行时之后,运行 “make python” 来建立两个可执行文件(实际上是 shell 脚本):add_person_python 和 list_people_python。它们的工作方式与 C++ 的可执行文件相同。
Java¶
按照 …/README.md 中的说明安装 protoc,然后从 maven 下载 protobuf Java 运行时 .jar 文件:
https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java
然后运行以下内容:
$ export CLASSPATH=/path/to/protobuf-java-[version].jar
$ make java
这将创建 add_person_java/list_people_java 可执行文件(shell 脚本),可用于创建/显示地址簿数据文件。
Go¶
Follow instructions in …/README.md to install protoc. Then install the Go protoc plugin (protoc-gen-go):
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
The “go install” command will install protoc-gen-go into the GOBIN
directory. You can set the
Build the Go samples with “make go”. This creates the following executable files in the current directory:
add_person_go list_people_go
To run the example:
./add_person_go addressbook.data
to add a person to the protocol buffer encoded file addressbook.data. The file is created if it does not exist. To view the data, run:
./list_people_go addressbook.data
Observe that the C++, Python, Java, and Dart examples in this directory run in a similar way and can view/modify files created by the Go example and vice versa.
Dart¶
First, follow the instructions in …/README.md to install the Protocol Buffer Compiler (protoc).
Then, install the Dart Protocol Buffer plugin as described here.
Note, the executable bin/protoc-gen-dart
must be in your PATH
for protoc
to find it.
Build the Dart samples in this directory with make dart
.
要运行这些例子:
$ dart add_person.dart addressbook.data
$ dart list_people.dart addressbook.data
The two programs take a protocol buffer encoded file as their parameter. The first can be used to add a person to the file. The file is created if it does not exist. The second displays the data in the file.