英文:
Import .proto files from another project
问题
我想将一个在不同项目中定义的.proto文件导入到我的golang项目中,并在其中使用定义的消息类型。
我想要导入的.proto文件是:https://github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto
我已经添加了导入语句:
import "github.com/lyft/clutch/api/k8s/v1/k8s.proto";
为了使用该文件中的消息类型"Job",我添加了以下内容:
message Jobs {
repeated clutch.k8s.v1.Job job = 1;
}
当我尝试编译.proto文件时,我遇到了以下错误:
导入"github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto"未找到或存在错误。
"clutch.k8s.v1.Job"未定义。
英文:
I want to import .proto file defined in different project into my golang project and use a message type defined in it.
Proto file I want to import is: https://github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto
I have added the import statement as :
import "github.com/lyft/clutch/api/k8s/v1/k8s.proto";
to use message type "Job" in that file, I added
message Jobs {
repeated clutch.k8s.v1.Job job = 1;
}
When I try to compile proto file, I'm getting the error
Import "github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto" was not found or had errors.
"clutch.k8s.v1.Job" is not defined.
答案1
得分: 2
为了编译您的proto文件,您需要克隆依赖的仓库,并将其设置为protoc
导入的包含路径,例如:
<kbd>job.proto</kbd>
syntax = "proto3";
package clutch.k8s.v1;
// 定义
option go_package = "github.com/job";
import "api/k8s/v1/k8s.proto";
message Jobs {
repeated clutch.k8s.v1.Job job = 1;
}
<kbd>compile.sh</kbd>
proto_out_dir=.
GOBIN=~/go/bin
protoc \
--go_out "${proto_out_dir}" \
--go_opt paths=source_relative \
--go-grpc_out "${proto_out_dir}" \
--go-grpc_opt require_unimplemented_servers=false,paths=source_relative \
--plugin protoc-gen-go="${GOBIN}/protoc-gen-go" \
--plugin protoc-gen-go-grpc="${GOBIN}/protoc-gen-go-grpc" \
--go-grpc_opt=paths=source_relative \
--proto_path=../protoc-gen-validate \
--proto_path=../api-common-protos \
--proto_path=../clutch \
--proto_path=../clutch/api \
--proto_path=. \
job.proto
将生成:
<kbd>job.pb.go</kbd>
...
type Jobs struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Job []*v1.Job `protobuf:"bytes,1,rep,name=job,proto3" json:"job,omitempty"`
}
...
还可以在源代码仓库中查看如何构建proto文件这里
英文:
In order to compile your proto, you should clone the dependency repos and set as include path in the protoc
import, as example:
<kbd>job.proto</kbd>
syntax = "proto3";
package clutch.k8s.v1;
// degine
option go_package = "github.com/job";
import "api/k8s/v1/k8s.proto";
message Jobs {
repeated clutch.k8s.v1.Job job = 1;
}
<kbd>compile.sh</kbd>
proto_out_dir=.
GOBIN=~/go/bin
protoc \
--go_out "${proto_out_dir}" \
--go_opt paths=source_relative \
--go-grpc_out "${proto_out_dir}" \
--go-grpc_opt require_unimplemented_servers=false,paths=source_relative \
--plugin protoc-gen-go="${GOBIN}/protoc-gen-go" \
--plugin protoc-gen-go-grpc="${GOBIN}/protoc-gen-go-grpc" \
--go-grpc_opt=paths=source_relative \
--proto_path=../protoc-gen-validate \
--proto_path=../api-common-protos \
--proto_path=../clutch \
--proto_path=../clutch/api \
--proto_path=. \
job.proto
Will produce:
<kbd>job.pb.go</kbd>
...
type Jobs struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Job []*v1.Job `protobuf:"bytes,1,rep,name=job,proto3" json:"job,omitempty"`
}
...
See also how the proto are build in the source repo here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论