A `.proto` file is the source of a Protocol Buffers contract. It declares messages for structured data and service functions that use those messages. In a microservice system, define the contract before generating client and server stubs, then implement the server logic and call it from a client. section 3.1 section 2.5.1
For an Order service to request a payment, a Payment contract can define `CreatePaymentRequest`, `CreatePaymentResponse`, and a `Create` function that accepts one and returns the other. The request and response types determine what the generated client can send and receive; message declarations alone do not define a callable service. section 2.5.1
syntax = "proto3"; #1
option go_package="github.com/huseyinbabal/grpc-
microservices-in-go/listing_2.1/payment"; #2
message CreatePaymentRequest {
float price = 1;
}
message CreatePaymentResponse {
int64 bill_id = 1;
}
service Payment {
RPC Create(CreatePaymentRequest)
returns (CreatePaymentResponse) {}
}
The Payment definition shows the proto3 declaration, a Go package option, two messages, and a unary service function. section 2.5.2
Run `protoc` with the Go and Go gRPC output options to generate code from the file. For `proto/order.proto`, message operations go into `order.pb.go`, while gRPC functionality goes into `order_grpc.pb.go`. The generated client exposes service-specific methods, leaving serialization and deserialization to the generated code. section 2.5.2 section 3.2.2
Listing 2.1 Command to generate source code for Go
protoc \
--go_out=. \
--go_opt=paths=source_relative \
--go-grpc_out=. \
--go-grpc_opt=paths=source_relative \
payment.proto
This command generates Go message code and gRPC code beside the input proto file using source-relative paths. section 2.5.2
Choose the function signature for the exchange you need: a unary call returns one response, while a stream can carry multiple responses, requests, or both. Proto files can live with a service, but a separate repository lets consumers in other languages generate stubs without depending on a Go production project. section 2.5.2 section 3.3


