How to Use Server Streaming RPC in Go

Web
Implement server streaming RPC in Go by defining a stream in your proto file and using the Send method in your server handler to push multiple responses.

Server streaming RPCs in Go are implemented by defining a server-side method that returns a server-side stream interface, which you then use to send multiple responses to a single client request. You define the service in your .proto file with a stream keyword on the response type, generate the Go code, and implement the server method to call Send on the stream until done.

func (s *myServer) MyStreamRPC(req *pb.MyRequest, stream pb.MyService_MyStreamRPCServer) error {
    for i := 0; i < 5; i++ {
        err := stream.Send(&pb.MyResponse{Value: i})
        if err != nil {
            return err
        }
    }
    return nil
}