go - 如何正确导入 protobuf 的文件?

标签 go protocol-buffers grpc proto

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版本:

go version go1.12.9 windows/amd64

协议(protocol)--版本:

libprotoc 3.11.4

回显 %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

正如您在命令中看到的,我指定了 src 文件夹的路径,protobuf 的文件所在的文件夹。

我还使用了 gogoprotobufgogofaster 插件包。

关于go - 如何正确导入 protobuf 的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61497404/

相关文章:

go - 将算法从 Python 移植到 Go

protocol-buffers - 如何在 autoconf/automake 中使用 Protocol Buffer ?

c++ - 如何从函数返回 grpc::ClientContext?

asp.net - 使用 ASP.NET Core 同时运行 REST API 和 gRPC

swift - Google Cloud Storage 是否有 gRPC 端点?

pointers - 为什么 2 个不同的 http.Request 结构的 http.Request.URL.Host 的地址相同?

postgresql - 从 golang 查询 Postgres 复合类型

go - 使用 ZeroMQ 在 Go 中处理中断的惯用方法

c# - .NET/C# 与 Python 互操作

Python和 Protocol Buffer : how to removed an element from a repeated message field