To set up Protobuf for your Go gRPC services, follow the steps below:
Begin by installing the Protobuf compiler and the necessary Go plugins:
# Install the Protobuf compiler
# For macOS
brew install protobuf
# For Ubuntu
sudo apt install protobuf-compiler
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest`
Create a gen
directory in your project to store the generated code:
mkdir gen
Use the protoc
command to generate Go code for your .proto
files. Run the following commands for each file:
protoc --go_out=gen --go-grpc_out=gen proto/user.proto
protoc --go_out=gen --go-grpc_out=gen proto/payment.proto`
In your main.go
files for both the User and Payment services, update the import paths to point to the generated code:
-
User Service (
user-service/main.go
):pb "path/to/your/project/gen/user"`
-
Payment Service (
payment-service/main.go
):`pb "path/to/your/project/gen/payment"`
After generating the code and updating the imports, build the services using the following commands:
# Build the Payment Service
go build -o ../tmp/payment-service-executable
go build -o ../tmp/user-service-executable
- Ensure that you have the correct path to your project in the import statements.
- The generated code will be placed in the
gen
directory. Verify that the files are created successfully. - Consider adding the
gen
directory to your.gitignore
file to avoid version control issues with generated code.