在使用 GoLand 编译 gRPC 的 .proto 文件后,出现了导入丢失的问题。

huangapple go评论70阅读模式
英文:

Missing imports after compiling gRPC .proto file for GO using GoLand

问题

我正在尝试解决一本gRPC书中的示例,但我不明白我做错了什么。

我有一个.proto文件,并且我可以将其编译成.pb.go存根文件。

我正在尝试构建客户端和服务器,文件夹结构如下所示:
在使用 GoLand 编译 gRPC 的 .proto 文件后,出现了导入丢失的问题。

我成功运行了这些命令,并为客户端和服务器生成了.pb.go文件,所以没有问题:

service % protoc -I ecommerce ecommerce/product_info.proto --go_out=plugins=grpc:./ecommerce

client % protoc -I ecommerce ecommerce/product_info.proto --go_out=plugins=grpc:./ecommerce

然而,虽然服务器的go文件可以解析所有导入,但客户端却不能。

在使用 GoLand 编译 gRPC 的 .proto 文件后,出现了导入丢失的问题。

从文件夹结构来看,我原以为一切都会顺利进行(我刚刚开始学习go和gRPC,所以请谅解)。

我的IDE是GoLand,我怀疑路径有问题,但我现在不知道从哪里开始。

英文:

I am trying to work out the example from a gRPC book, and I struggle to get what's wrong with what I am doing.

I have a .proto file, and I can compile it into the .pb.go stub.

I am trying to build both the client and the server, and the folders structure looks as following:
在使用 GoLand 编译 gRPC 的 .proto 文件后,出现了导入丢失的问题。

I run these commands successfully and I generate the .pb.go files for both client and server, so no issue with that:

service % protoc -I ecommerce ecommerce/product_info.proto --go_out=plugins=grpc:./ecommerce

client % protoc -I ecommerce ecommerce/product_info.proto --go_out=plugins=grpc:./ecommerce

However, while the server's go file can resolve all the imports, the same doesn't happen for the client.

在使用 GoLand 编译 gRPC 的 .proto 文件后,出现了导入丢失的问题。

Looking at the folder structure, I was expecting everything to work out (I just started this week with go and gRPC, so please be understanding).

My IDE is GoLand, I suspect there is something wrong with the paths, but I wouldn't know where to start right now.

答案1

得分: 1

根据评论,截图显示service文件夹中有一个go.mod文件,但client文件夹中没有。由于Goland将启用“Go模块集成”,它将期望一个模块(我强烈建议使用模块!)。

最简单的解决方法是在client文件夹中运行go mod init [module-path](然后运行go mod tidy)。你也可以在backend文件夹(或productinfo文件夹)中建立一个模块,这样它将被子文件夹共享。

英文:

As per the comments the screenshot showed a go.mod in service but none in client. As goland will be running with 'Go Modules Integration' enabled it will be expecting a module (I'd highly recommend using modules!).

The simplest fix is to run go mod init [module-path] (followed by go mod tidy) in the client folder. You could also establish a module in the backend folder (or productinfo folder) and that will be shared by child folders.

huangapple
  • 本文由 发表于 2021年7月23日 05:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68491893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定