Last active
December 16, 2020 01:24
-
-
Save brianpgerson/2c4aeadc512c0bba1edbf3887165b3d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
pb "envoy_example/protos" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/metadata" | |
"google.golang.org/grpc/status" | |
) | |
func main() { | |
cc, err := grpc.Dial("localhost:5050", grpc.WithInsecure()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer cc.Close() | |
client := pb.NewHelloServiceClient(cc) | |
request := &pb.HelloRequest{Name: "brian"} | |
ctx := metadata.AppendToOutgoingContext(context.Background(), "Authorization", "Bearer foo", "Bar", "baz") | |
resp, err := client.Hello(ctx, request) | |
if err != nil { | |
errStatus, isGrpcErr := status.FromError(err) | |
if !isGrpcErr { | |
fmt.Printf("Unknown error! %v", errStatus.Message()) | |
return | |
} | |
code := errStatus.Code() | |
msg := errStatus.Message() | |
fmt.Println(code) | |
fmt.Println(msg) | |
} else { | |
fmt.Printf("Receive response => [%v]", resp.Greeting) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment