Skip to content

Instantly share code, notes, and snippets.

@f3nry f3nry/main.go
Last active Feb 12, 2019

What would you like to do?
Simple Go gRPC Client
package main
import (
var (
tls = flag.Bool("tls", true, "Connection uses TLS if true, else plain TCP")
caFile = flag.String("ca_file", "", "The file containning the CA root cert file")
serverAddr = flag.String("server_addr", "<api-gateway-host>:443", "The server address in the format of host:port")
serverHostOverride = flag.String("server_host_override", "<api-gateway-host>", "The server name use to verify the hostname returned by TLS handshake")
func main() {
var opts []grpc.DialOption
if *tls {
creds, err := credentials.NewClientTLSFromFile(*caFile, *serverHostOverride)
if err != nil {
log.Fatalf("Failed to create TLS credentials %v", err)
opts = append(opts, grpc.WithTransportCredentials(creds))
opts = append(opts, grpc.WithWaitForHandshake())
} else {
opts = append(opts, grpc.WithInsecure())
conn, err := grpc.Dial(*serverAddr, opts...)
if err != nil {
log.Fatalf("fail to dial: %v", err)
defer conn.Close()
client := api.NewProdClient(conn)
resp, err := client.Alive(context.TODO(), &api.AliveRequest{Message: "Hello"})
if err != nil {
log.Fatalf("err making request: %v", err)
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.