Skip to content

Instantly share code, notes, and snippets.

@harche
Forked from pramonow/client.go
Created February 22, 2022 07:32
Show Gist options
  • Save harche/f82e76602d3f67531bd4753043356bf9 to your computer and use it in GitHub Desktop.
Save harche/f82e76602d3f67531bd4753043356bf9 to your computer and use it in GitHub Desktop.
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