How do you define a gRPC service in a `.proto` file?
Answer
A gRPC service is defined in a .proto file using the service keyword. Example: syntax = "proto3"; package example; service ProductService { rpc GetProduct(GetProductRequest) returns (Product); rpc ListProducts(ListProductsRequest) returns (stream Product); rpc CreateProduct(CreateProductRequest) returns (Product); } message Product { int32 id = 1; string name = 2; float price = 3; } message GetProductRequest { int32 id = 1; }. Key rules: each rpc method has exactly one request type and one return type; streaming is indicated with the stream keyword before the type; field numbers (e.g., = 1) must be unique within a message and are permanent once deployed; use package to namespace your messages. After defining the service, run protoc --grpc_out=. --go_out=. service.proto to generate language-specific code.