首页 > 其他分享 >protobuf

protobuf

时间:2023-07-09 22:11:08浏览次数:27  
标签:protobuf proto demo stu base student name

 

// protobuf
// 跨语言、跨平台的序列化数据结构的方式,用于序列化数据的协议
// 类似于xml、json


///////////////////示例//////////////////////////////////////////////
/*
    在apollo/cyber目录下新建文件夹demo_base_proto,文件夹下新建student.proto,并输入
    如下内容
*/

// proto版本
syntax = "proto2";

// 包
package apollo.cyber.demo_base_proto; //.到当前目录

// 消息  message是关键字, Student是消息名称
message Student{
    // 字段
    // 字段格式:字段规则 数据类型 字段名称 字段编号
    required string name = 1;
    optional uint64 age = 2;
    optional uint64 height = 3;
    repeated string books = 4;
}
////////////////////////////////////////////////////////////////


/*
proto 中的字段语法主要有四部分组成:
字段规则、数据类型、字段名称、字段编号

1.字段规则

required--调用时,必须提供该字段的值,否则该消息将被视为“未初始化”,不建议使用,当需要把字段修改为其他规则时,会存在兼容性问题。
optional- 调用时该字段的值可以设置也可以不设置,不设置时,会根据数据类型生成默认值
repeated-该规则字段可以以动态数组的方式存储多个数据

2.数据类型
double float int32 int64 uint32 uint64 string bool

3.字段名称
就是变量名

4.字段编号
每个字段有唯一的编号,用于在消息的二进制格式中标识字段

*/


//////////////////////文件编译///////////////////////////
//  1.编辑BUILD文件

package(default_visibility = ["//visibility:public"])
//  创建proto的library
proto_library(
    name = "student_proto",
    srcs = "student.proto"
)
//  创建cc的library
cc_library(
    name = "student_cc",
    deps = ":student_proto"
)
//  2.编译
bazel build cyber/demo_base_proto/...

// 编译后,将会在cyber/demo_base_proto生成可以被c++调用的文件

/////////////////////////////////////////////////////////////



///////////////////////proto文件操作/////////////////////////
// 1.在demo_base_proto目录下新建test_student.cc文件,输入内容如下:
#include "cyber/demo_base_proto/student.pb.h"

int main(int argc, char* argv[])
{
    // 创建对象
    apollo::cyber::demo_base_proto::Student stu;
    // 数据写
    stu.set_name("zhangsan"); // requried 和 optinoal都是set_
    stu.set_age(18);
    stu.set_height(1.75);
    stu.add_books("yuwen"); // repeated是add_
    stu.add_books("c++");
    stu.add_books("Python");

    // 数据读
    std::string name = stu.name();
    uint64_t age = stu.age();
    uint64_t height = stu.height();
    std::cout << name << " == " << age << " == " << height << " == ";
    for(int i = 0 ; i < stu.books_size(); i++)
    {

        std::cout << stu.books(i) " - ";

    }
    std::cout << std::endl;



    return 0;
}

// 2.配置BUILD文件

cc_binary(
    name = "test_student_cc",
    srcs = ["test_student.cc"],
    deps = [":student_cc"]

)

// 3.编译
bazel build cyber/demo_base_proto/...

// 4.执行
./bazel-bin/cyber/demo_base_proto/test_student

 

标签:protobuf,proto,demo,stu,base,student,name
From: https://www.cnblogs.com/WTSRUVF/p/17539538.html

相关文章

  • 前端使用protobuf进行传参
    一.proto的理解1.以.proto结尾的文件是protobuf文件,且.proto是一种传参规则的定义。2.常用的请求传参方式是json或xml,因为在大多数的语言中这两种轻量型语言都能被其他语言识别到(java、python、javascript、c++等等)。proto作为区别于前两者且类似于前两者的语言:*它是一种语言......
  • Kubernetes编程——修改客户端默认支持 Protobuf
    修改客户端默认支持Protobuf一、在kubernetes客户端中修改默认支持Protobuf确保你已经安装了kubectl命令行工具,并且版本在1.14.0或更高。打开~/.kube/config文件,该文件存储了你的Kubernetes集群配置信息。找到clusters部分,并在你的集群配置下添加extensions字段,示例如下:......
  • protobuf学习
    下载Protobuf下载地址:https://github.com/protocolbuffers/protobuf/releasesPython安装Protobuf在python中使用protobuf,还需要安装python对应的protobuf包(否则会报错:Nomodulenamedgoofgle):pipinstallprotobuf==3.12.0proto文件语法messagePerson{ requiredstringn......
  • mac 下Golang 安装Protobuf
    1、安装protobufbrewinstallprotobuf2、检查安装结果protoc--version3、安装golangforprotobuf插件gogetgithub.com/golang/protobuf/protoc-gen-gogoget-u-vgithub.com/golang/protobuf/protoc-gen-gogoget=gitclone+goinstall这里会慢的要死所以我这里采取......
  • protobuf教程(二)---引入其他proto文件
    被引入的response.proto的文件:syntax="proto3";packageresponse;optiongo_package="github.com/TripleCGame/apis/api/response;response";import"google/protobuf/struct.proto";messageResponse{int32code=1;google.prot......
  • protobuf 中go_package的意思
    一个简单的protobuf文件定义如下:response.protosyntax="proto3";optiongo_package="github.com/TripleCGame/apis/api;api";import"google/protobuf/struct.proto";messageResponse{int32code=1;google.protobuf.Structdata......
  • golang 学习之 etcd protobuffer grpc gorm 服务注册发现 go-micro
    1.etcd使用步骤1)下载:https://github.com/etcd-io/etcd/releases/2)配置环境变量3)编辑local-cluster-profile文件:(利用goreman启动方式,生产环境参考官方文档)etcd1:etcd--nameinfra1--listen-client-urlshttp://127.0.0.1:2379--advertise-client-urlshttp://127.0.0.1......
  • go语言中protobuf以及grpc的使用
    首先定义数据结构,保存为.proto文件syntax="proto3";packagetutorial;//Theprotocolcompilergeneratesaclassfromthefollowing.protofilewith//methodsforwritingandreadingthemessagefields.Theclassiscalled//"Person"andisin......
  • [原]Android上GTalk以及Push机制的XMPP数据选择使用protobuf格式而非XML格式
    [url]http://code.google.com/p/protobuf/[/url][b]先介绍下什么是protobuf以及有什么好处.[/b][i]ProtocolBuffersareawayofencodingstructureddatainanefficientyetextensibleformat.GoogleusesProtocolBuffersforalmostallofit......
  • ProtoBuf简介
    proto简介一、protobuf的定义protobuf是一种用于序列化结构数据的工具,实现数据的存储与交换,与编程语言和开发平台无关。序列化:将结构数据或者对象转换成能够用于存储和传输的格式。反序列化:在其他的计算环境中,将序列化后的数据还原为结构数据和对象。定义数据的结构,然后使用p......