Skip to content

Instantly share code, notes, and snippets.

@jasonsalas
Created August 16, 2021 19:07
Show Gist options
  • Save jasonsalas/ff7a79b5b40fdd34ff1a465d6b862513 to your computer and use it in GitHub Desktop.
Save jasonsalas/ff7a79b5b40fdd34ff1a465d6b862513 to your computer and use it in GitHub Desktop.
gRPC server-side streaming in Go - client package
package transaction
import (
"context"
"io"
"log"
bank "github.com/jasonsalas/protobank/pkg/protobuf/bank"
"google.golang.org/grpc"
)
type Client struct {
client bank.TransactionServiceClient
}
func NewClient(conn grpc.ClientConnInterface) Client {
return Client{
client: bank.NewTransactionServiceClient(conn),
}
}
func (c Client) Fetch(ctx context.Context, accountID string) error {
stream, err := c.client.Fetch(ctx, &bank.FetchRequest{AccountId: accountID})
if err != nil {
return err
}
for {
response, err := stream.Recv()
if err != nil {
if err == io.EOF {
log.Println("> ALL DONE!")
return nil
}
return err
}
trx := response.GetTransaction()
log.Println("TIME:", trx.GetTime().String())
log.Println("OPERATION:", trx.GetOperation().String())
log.Println("AMOUNT:", trx.GetAmount())
log.Println("------")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment