Hone logo
Hone
Problems

Building a gRPC Client in Go for a Simple Calculator Service

This challenge will guide you through creating a gRPC client application in Go. You will interact with a pre-existing gRPC server (which you don't need to build) that exposes a simple calculator service. This exercise is fundamental for understanding inter-service communication in modern distributed systems.

Problem Description

Your task is to develop a Go program that acts as a gRPC client. This client will connect to a running gRPC calculator server and invoke its defined RPC methods. Specifically, you need to implement a client that can:

  1. Add two numbers: Send two integers to the server and receive their sum.
  2. Subtract two numbers: Send two integers to the server and receive their difference.
  3. Multiply two numbers: Send two integers to the server and receive their product.
  4. Divide two numbers: Send two integers to the server and receive their quotient. This RPC should also handle the edge case of division by zero gracefully.

You will be provided with the Protocol Buffers (.proto) definition file for the calculator service. You will use this definition to generate Go code for both the client and server interfaces, and then implement the client logic.

Key Requirements:

  • The client must successfully connect to a gRPC server listening on localhost:50051 (default address).
  • The client must implement functions to call each of the calculator service's RPC methods (Add, Subtract, Multiply, Divide).
  • The client should handle potential errors returned by the server, such as division by zero.
  • The client program should demonstrate calling each RPC method with sample inputs and printing the results.

Expected Behavior:

When the client program is executed, it should:

  • Establish a connection to the gRPC server.
  • Call the Add, Subtract, Multiply, and Divide RPCs with predefined sample inputs.
  • Print the results of each operation to the standard output in a clear, readable format.
  • If a division by zero occurs, the client should catch the gRPC error and print an informative message.

Examples

Let's assume the following .proto definition for the calculator service:

syntax = "proto3";

package calculator;

service Calculator {
  rpc Add (AddRequest) returns (AddResponse) {}
  rpc Subtract (SubtractRequest) returns (SubtractResponse) {}
  rpc Multiply (MultiplyRequest) returns (MultiplyResponse) {}
  rpc Divide (DivideRequest) returns (DivideResponse) {}
}

message AddRequest {
  int32 a = 1;
  int32 b = 2;
}

message AddResponse {
  int32 result = 1;
}

message SubtractRequest {
  int32 a = 1;
  int32 b = 2;
}

message SubtractResponse {
  int32 result = 1;
}

message MultiplyRequest {
  int32 a = 1;
  int32 b = 2;
}

message MultiplyResponse {
  int32 result = 1;
}

message DivideRequest {
  int32 a = 1;
  int32 b = 2;
}

message DivideResponse {
  double result = 1; // Using double for potential floating-point division
}

Example 1: Successful Addition

Input: Add(a=5, b=3)
Output:
Addition: 5 + 3 = 8

Example 2: Division by Zero Handling

Input: Divide(a=10, b=0)
Output:
Division by zero error: rpc error: code = InvalidArgument desc = cannot divide by zero

Example 3: Successful Subtraction and Multiplication

Input: Subtract(a=10, b=4), Multiply(a=6, b=7)
Output:
Subtraction: 10 - 4 = 6
Multiplication: 6 * 7 = 42

Constraints

  • The gRPC server will be available at localhost:50051.
  • You must use the provided .proto file to generate Go code using protoc.
  • Your client application should be written entirely in Go.
  • The generated Go code from the .proto file should be placed in a subdirectory (e.g., pb) and imported into your main client package.
  • Error handling for gRPC calls is mandatory.

Notes

  • You will need to install the protobuf compiler and the Go gRPC plugins.
  • The protoc command will generate Go files that contain the necessary structs, interfaces, and client stubs.
  • Remember to import the generated Go package into your main client code.
  • The grpc.Dial function is crucial for establishing a connection.
  • Error handling in gRPC clients often involves checking the error return value from RPC calls.
  • Consider how to handle the different response types from the server.
Loading editor...
go