Skip to content

Instantly share code, notes, and snippets.

@jamisonhyatt
Created March 22, 2018 02:24
Show Gist options
  • Save jamisonhyatt/dd9d93b978472c960c9fa81b9f39fcf9 to your computer and use it in GitHub Desktop.
Save jamisonhyatt/dd9d93b978472c960c9fa81b9f39fcf9 to your computer and use it in GitHub Desktop.
gRPC Client Example
package main
import (
"context"
"fmt"
"log"
"github.com/jamisonhyatt/grpc-multi-pkg-protos/pkg/external/location"
"github.com/jamisonhyatt/grpc-multi-pkg-protos/pkg/weatherman"
desktop "github.com/jamisonhyatt/grpc-multi-pkg-protos/pkg/weatherman/desktop_svc"
mobile "github.com/jamisonhyatt/grpc-multi-pkg-protos/pkg/weatherman/mobile_svc"
"google.golang.org/grpc"
)
func main() {
var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
conn, err := grpc.Dial("localhost:8000", opts...)
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
mobileClient := mobile.NewMobileClient(conn)
desktopClient := desktop.NewDesktopClient(conn)
weathermanClient := weatherman.NewWeathermanClient(conn)
health, err := weathermanClient.Healthcheck(context.Background(), &weatherman.HealthCheckRequest{})
if err != nil || !health.Healthy {
panic("uhoh")
}
fmt.Printf("health: %v\n", health)
mobileWeather, err := mobileClient.GetWeather(context.Background(), &mobile.GetWeatherRequest{City: &location.City{
Name: "Seattle", State: location.State_WASHINGTON,
}})
if err != nil || !mobileWeather.Forecast.Rain {
panic("definitely not right")
}
fmt.Printf("Mobile Client Seattle Weather: %v\n", mobileWeather)
desktopWeather, err := desktopClient.GetWeather(context.Background(), &desktop.GetWeatherRequest{City: &location.City{
Name: "Seattle", State: location.State_WASHINGTON,
}})
if err != nil || !desktopWeather.Forecast.Rain {
panic("definitely not right")
}
fmt.Printf("Desktop Client Seattle Weather: %v\n", desktopWeather)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment