php protobuf3,初识protobuf3

- [protocbuf文档](https://developers.google.cn/protocol-buffers/)

- [protoc3 指南](https://developers.google.cn/protocol-buffers/docs/proto3)

- 安装protoc

[下载地址](https://github.com/protocolbuffers/protobuf/releases)

- 安装 protoc-gen-go

*安装后要添加到环境变量中能让protoc程序找到*

```bash

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

```

- 定义一个消息类型

新建search.proto 文件内容如下

```protobuf

// 定义一个搜索请求

// syntax 必须是非空注释的第一行

syntax = "proto3"; // 制定解析工具是 proto3 如果不指定默认 proto2

message SearchRequest {

string query = 1;

int32 page_number =2;

int32 result_per_page = 3;

}

```

编译

```bash

protoc --go_out=. ./protoc/search.proto

```

proto 支持的数据类型

|.proto Type| NOtes| go|php|

|:---:|:---:|:---:|:---:|

|double| |float64|float|

|float| |float32|float|

|int32|如果有负号请使用sint32 |int32|integer|

|int64|如果有负号请使用sint54 |int64|integer/string|

|uint32| 使用变长编码|uint32|integer|

|uint64|使用变长编码 |uint64|integer/string|

|sint32| 使用变长编码,有符号的整型值。编码时比通常的int32高效。|int32|integer|

|sint64| 使用变长编码,有符号的整型值。编码时比通常的int64高效。|int64|integer/string|

|fixed32| 总是8个字节,如果值总是比228大的话,这个类型比uint32高效|uint32|integer|

|fixed64| 总是8个字节,如果值总是比256大的话,这个类型比uint64高效|uint64|integer/string|

|sfixed32|总是4个字节 |int32|integer|

|sfixed64| 总是8个字节|int64|integer/string|

|bool| |bool|boolean|

|string|一个字符串必须是UTF-8编码或者7-bit ASCII编码的文本。 |string|string|

|bytes|可能包含任意顺序的字节数据 |[]byte|string|

目前还没有正式实战,等实战完回来继续优化

有疑问加站长微信联系(非本文作者))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值