Skip to content

Instantly share code, notes, and snippets.

@pramonow

pramonow/client.go

Last active Nov 16, 2020
Embed
What would you like to do?
package main
import (
"context"
"io"
"log"
pb "github.com/pramonow/go-grpc-server-streaming-example/src/proto"
"google.golang.org/grpc"
)
func main() {
// dial server
conn, err := grpc.Dial(":50005", grpc.WithInsecure())
if err != nil {
log.Fatalf("can not connect with server %v", err)
}
// create stream
client := pb.NewStreamServiceClient(conn)
in := &pb.Request{Id: 1}
stream, err := client.FetchResponse(context.Background(), in)
if err != nil {
log.Fatalf("open stream error %v", err)
}
done := make(chan bool)
go func() {
for {
resp, err := stream.Recv()
if err == io.EOF {
done <- true //means stream is finished
return
}
if err != nil {
log.Fatalf("cannot receive %v", err)
}
log.Printf("Resp received: %s", resp.Result)
}
}()
<-done //we will wait until all response is received
log.Printf("finished")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.