如何正确导入 protobuf 的文件?
How correctly import protobuf's files?
在 city.proto
文件中我想使用(导入)protobuf 的 .proto
文件。在我的 golang 应用程序中,我使用 go modules。
city.proto:
syntax = "proto3";
package proto;
import "google/protobuf/timestamp.proto";
option go_package = "./proto";
message City {
google.protobuf.Timestamp create_at = 1;
}
当我尝试从 city.proto
文件生成代码时,它引发了这样的错误:
google/protobuf/timestamp.proto: File not found.
city.proto:3:1: Import "google/protobuf/timestamp.proto" was not found or had errors.
city.proto:25:5: "google.protobuf.Timestamp" is not defined.
我在我的 gRPC 项目目录中创建了一个 proto
文件夹。 city.proto
文件位于此文件夹中。我运行这样的命令:
protoc -I proto/ proto/city.proto --go_out=plugins=grpc:proto/city.
此命令仅在我不在 proto 文件中使用 import 的情况下有效。
去版本:
go version go1.12.9 windows/amd64
协议--版本:
libprotoc 3.11.4
echo %GOPATH%:
C:\Users\NNogerbek\go
在该目录中,我看到三个具有这种结构的文件夹:
bin
protoc.exe
protoc-gen-go.exe
pkg
mod
**packages**
src
google.com
protobuf
timestamp.proto
我运行这样的命令:
go list -f "{{ .Path }} {{ .Dir }}" -m github.com/golang/protobuf
命令结果:
github.com/golang/protobuf C:\Users\NNogerbek\go\pkg\mod\github.com\golang\protobuf@v1.4.0
经过多次尝试,我找到了一种方法,可以使用以下命令在我的 gRPC 服务器上无错误地生成 go 代码:
protoc -I. -I%GOPATH%/src --gogofaster_out=plugins=grpc:. proto/city.proto
如您在命令中所见,我指定了 protobuf 文件所在的 src
文件夹的路径。
我还使用了 gogoprotobuf 包的 gogofaster
插件。
在 city.proto
文件中我想使用(导入)protobuf 的 .proto
文件。在我的 golang 应用程序中,我使用 go modules。
city.proto:
syntax = "proto3";
package proto;
import "google/protobuf/timestamp.proto";
option go_package = "./proto";
message City {
google.protobuf.Timestamp create_at = 1;
}
当我尝试从 city.proto
文件生成代码时,它引发了这样的错误:
google/protobuf/timestamp.proto: File not found.
city.proto:3:1: Import "google/protobuf/timestamp.proto" was not found or had errors.
city.proto:25:5: "google.protobuf.Timestamp" is not defined.
我在我的 gRPC 项目目录中创建了一个 proto
文件夹。 city.proto
文件位于此文件夹中。我运行这样的命令:
protoc -I proto/ proto/city.proto --go_out=plugins=grpc:proto/city.
此命令仅在我不在 proto 文件中使用 import 的情况下有效。
去版本:
go version go1.12.9 windows/amd64
协议--版本:
libprotoc 3.11.4
echo %GOPATH%:
C:\Users\NNogerbek\go
在该目录中,我看到三个具有这种结构的文件夹:
bin
protoc.exe
protoc-gen-go.exe
pkg
mod
**packages**
src
google.com
protobuf
timestamp.proto
我运行这样的命令:
go list -f "{{ .Path }} {{ .Dir }}" -m github.com/golang/protobuf
命令结果:
github.com/golang/protobuf C:\Users\NNogerbek\go\pkg\mod\github.com\golang\protobuf@v1.4.0
经过多次尝试,我找到了一种方法,可以使用以下命令在我的 gRPC 服务器上无错误地生成 go 代码:
protoc -I. -I%GOPATH%/src --gogofaster_out=plugins=grpc:. proto/city.proto
如您在命令中所见,我指定了 protobuf 文件所在的 src
文件夹的路径。
我还使用了 gogoprotobuf 包的 gogofaster
插件。