Errors I encountered while working with gRPC

Tram Ho

Topic: Errors I encountered when working with gRPC (Part 1)


I am a newbie working with gRPC. The experiences are almost zero. This is the series of articles I will write about the errors I encountered when I started learning about gRPC. Hopefully other new friends like me if having the same error can save time.


1. Error writing generate code from .proto file
When I generate the client/server stubs file, use the protoc command as follows:

The last line is the path of the proto file we want to generate code from. Flag “go_out” yourself to “.” The purpose is for the generated code to be in the directory with the proto file. Above, I used more flags –go_opt=paths=source_relative ie when the code is generated, we can import the package of this code in the main.go file according to the path declared above. Here I use source_relative, it will implicitly be the path of the .proto file. After running the above command, I get the following error:
protoc-gen-go: unable to determine Go import path for "ecommerce/product_info.proto" What is the cause? As we have worked with golang, Gopath is extremely important to determine the path of packages. Here we can use go.mod to be able to work in any directory outside of Gopath. The above error means that when we generate code from the product_info.proto file, we need to import some more packages or dependencies of the file to be imported to generate the file. So it needs to determine our Gopath in the local machine.
Fix to add paragraph
option go_package = "Your Gopath";
Go to the .proto file and you’re done.
After generating the code successfully, you may encounter a package could not import error in the newly generated .go file, you will fix it by calling “go get name_package”.

Kết Luận

Ok finished an error today. This is a very basic error but it will take a lot of time if it is a golang and gRPC fuzzy chicken like me. Wish you all good study

Share the news now