【问题标题】:When I'm trying to compile the Protobuf in golang, It's showing '"int" is not defined.'当我尝试在 golang 中编译 Protobuf 时,它显示“未定义”。
【发布时间】:2022-07-02 19:38:45
【问题描述】:
在编译 proto 文件时,我得到'"int" is not defined'。
'test.proto' 文件
syntax = "proto3";
package test;
option go_package = "/;test";
message User {
string FirstName = 1;
string LastName = 2;
string Address = 3;
int Contact = 4;
int Age = 5;
}
Output:
test.proto:11:5: "int" is not defined.
【问题讨论】:
标签:
go
protocol-buffers
protobuf-java
protobuf-c
protobuf-go
【解决方案1】:
在 Protobuf 中,如果您使用语法为 'proto3',那么您只能在 Java/Kotlin、Python、C# 语言中使用 'int' 数据类型。对于 C++, Go 你必须使用 'int32' 对于 PHP 类型你必须使用 'integer' 对于 Ruby 类型你必须使用 ' Fixnum 或 Bignum(根据需要)' 根据字段的类型。
'int 32' - 使用可变长度编码。编码负数效率低 - 如果您的字段可能有负值,请改用 sint32。
Golang 的工作代码:
syntax = "proto3";
package test;
option go_package = "/;test";
message User {
string FirstName = 1;
string LastName = 2;
string Address = 3;
int32 Contact = 4;
int32 Age = 5;
}