syntax = "proto3"; // 字段的 tag // 1到15 只占用一个字节,所以应该用在不频繁使用的字段上。 import "data.proto" //引用其他的包 package my.project; // c# namespace My.Project option csharp_namespace = "My.WebApis"; // C# 的话生成的命名空间就是 =My.WebApis,其他的还是 my.project message Preson { int32 id = 1; string name = 2; float height = 3; float weight = 4; bytes avatar = 5; string email = 6; bool email_verified = 7; repeated string phone_numbers = 8; //数组的话,使用paked,(一个人可以有多个电话号码) Gender gender = 11; Date birthday = 12; repeated Address addresses = 13; //方式可能是个复数 reserved 9, 10, 20 to 100; //保留的tag,不能再用了 reserved "foo", "bar"; //保留的字段 enum Gender { option allow_alias = true; //起好别名了 NOT_SPECIFIED = 0; //未指定 FEMALE = 1; //男 MALE = 2; //女 WOMAN = 1; //男 MAN = 2; //女 } message Address { string province = 1; string city = 2; string zip_code = 3; string street = 4; string number = 5; } }
syntax = "proto3"; message Date { int32 year = 1; int32 month = 2; int32 day = 3; }
生成csharp文件的命令
protoc first.proto --csharp_out=csharp
下载 go 的依赖
go get -u github.com/golang/protobuf/protoc-gen-go
syntax = "proto3"; // package example.first; package src; option go_package = "./src"; //输出目录 message PresonMessage { int32 id = 1; bool is_adult = 2; string name = 3; repeated int32 lucky_number = 4; }
生成go的 proto 命令
protoc --go_out=. person.proto // 执行命令