syntax="proto3";packageecho;optiongo_package="demo/echo";messageEchoRequest{stringtext=1;boolcapitalize=2;}messageEchoResponse{stringtext=1;}serviceEchoService{// Echo returns the received text and make it louder too!rpcEcho(EchoRequest)returns(EchoResponse);}
Buf is a tool that simplifies the development and consumption of Protobuf APIs.
One of the features of Buf is managing dependencies and building proto files.
If you decide to use Buf, follow the instructions below or go to the protoc tab to see instructions using protoc.
Let's create a buf.gen.yaml with the following content:
packagemainimport("context""demo/gen/demo/echo""log""net""strings""google.golang.org/grpc")typeServicestruct{echo.UnimplementedEchoServiceServer}func(Service)Echo(ctxcontext.Context,req*echo.EchoRequest)(*echo.EchoResponse,error){response:=&echo.EchoResponse{Text:req.Text,}ifreq.Capitalize{response.Text=strings.ToUpper(response.Text)}returnresponse,nil}funcmain(){listener,err:=net.Listen("tcp",":40000")iferr!=nil{log.Fatalf("failed to listen: %s",err)}server:=grpc.NewServer()echo.RegisterEchoServiceServer(server,Service{})iferr:=server.Serve(listener);err!=nil{log.Fatalf("gRPC server failed: %v",err)}}