# Implement the client in GO

gRPC is designed to work across different languages and platforms. In this section, we will write a client in **GO** and invoke the `CalculatorServer` which we have implemented in **Java.**

## Learning GO

To learning the basic of GO, you can go through the tutorial below.\
[https://tour.golang.org/basics/6](https://tour.golang.org/basics/6)

## Initialize our GO project with GO module

We use GO module to manage our GO client code and library dependency.&#x20;

1. Create a folder **goclient** in our workspace to store all of our GO code:

```bash
cd ~/grpclab
mkdir goclient
```

2\.  Execute the following command to initialize a new GO module for us:

```bash
cd goclient
go mod init cse.cslab/goclient
```

## Generate the client stub for GO by using .proto

We reuse our `calculator.proto` to generate the client stub for our GO client by using protobuf compiler, `protoc`. Execute the following commands:

```bash
mkdir calculator

#protoc [target .proto] 
#         -I [the directory of .proto] 
#         --go_out=plugins=grpc:[destination of the generated code]
protoc ../src/main/proto/calculator.proto \
        -I ../src/main/proto \
        --go_out=plugins=grpc:./calculator
```

You will see `calculator/calculator.pb.go` is generated.

## Write your GO simple client

Now we write the main for our GO client:

```bash
nano main.go
```

```go
package main

import (
    "context"
    "fmt"
    "cse.cslab/goclient/calculator"
    "google.golang.org/grpc"
    "os"
)

func main() {
    //Create a grpc connection to `CalculatorServer`
    conn, err := grpc.Dial("127.0.0.1:8980", grpc.WithInsecure(), grpc.WithBlock())
    if err != nil{
        println("Can't create connection to server !!");
        os.Exit(-1)
    }
    //Create a stub of Calculator service for invoking the method of Calculator service later on
    client := calculator.NewCalculatorClient(conn)

    // Calculate "1 + 1" by calling the CalculatorService
    var a float32 =  1.0
    var b float32 = 1.0
    //Create the `CalculationRequest` object and set the value of `a` and `b`
    requestObj := calculator.CalculationRequest{A:a, B:b}
    //Invoking the `sum` function of `CalculatorService` through the stub
    respondObj, err := client.Sum(context.Background(), &requestObj)
    fmt.Printf("GoClient: The result of %f + %f = %f \n", a, b, respondObj.Result)

    // Calculate "3 x 2" by calling the CalculatorService
    a = 3.0
    b = 2.0
    requestObj = calculator.CalculationRequest{A:a, B:b}
    respondObj, err = client.Product(context.Background(), &requestObj)
    fmt.Printf("GoClient: The result of %f x %f = %f \n", a, b, respondObj.Result)
}
```

## Compile your program

Before compilation, we need to get some packages for GO:

```
go get github.com/golang/protobuf/proto
go get golang.org/x/net/context
go get google.golang.org/grpc
```

Now we can compile our GO code into binary `main`:

```bash
go build -o goclient
```

You will see that an executable file `goclient` is generated under your workspace.

## Test your GO Client

Now we have the `CalculatorServer` and `goclient`. We can test our programs.

Launch the `CalculatorServer` in the background:

```
java -cp ~/grpclab/target/grpclab-1-jar-with-dependencies.jar cslab.grpclab.CalculatorServer &
```

Launch the `goclient`:

```bash
./goclient
```

You should see the following output from `goclient`:

```
GoClient: The result of 1.000000 + 1.000000 = 2.000000
GoClient: The result of 3.000000 x 2.000000 = 6.000000
```
