英文:
Missing imports after compiling gRPC .proto file for GO using GoLand
问题
我正在尝试解决一本gRPC书中的示例,但我不明白我做错了什么。
我有一个.proto文件,并且我可以将其编译成.pb.go存根文件。
我成功运行了这些命令,并为客户端和服务器生成了.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文件可以解析所有导入,但客户端却不能。
从文件夹结构来看,我原以为一切都会顺利进行(我刚刚开始学习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:
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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论