Use Object List with gRPC in Go Microservices

The latest release of Protocol Buffers can be found on the release page https://github.com/protocolbuffers/protobuf/releases/tag/v3.9.1

Make sure Git is installed on your machine and in your system’s PATH. Install the package to your $GOPATH with the go tool from shell:

$ go get google.golang.org/grpc




Create new folder named src. In src folder, create new folder named productservice. In productservice folder, create new folder named proto. In proto folder, create new file named product.proto as below:

syntax = "proto3";

package productservice;

service ProductService {
    rpc FindAll (FindAllRequest) returns (FindAllResponse);
}

message FindAllRequest {
}

message FindAllResponse {
    repeated Product Products = 1;
}

message Product {
    string id = 1;
    string name = 2;
    int32 quantity = 3;
    double price = 4;
    bool status = 5;
}

Select productservice folder and open it in Terminal window of Visual Studio Code. Use command line as below to generate product.pb.go file:

protoc -I ./ proto/product.proto --go_out=plugins=grpc:.




In productservice folder, create new folder named handlers. In this folder, create new file named handler.go as below:

package handlers

import (
	"context"
	productservice "productservice/proto"
)

type ProductServiceServer struct {
}

func (*ProductServiceServer) FindAll(ctx context.Context, in *productservice.FindAllRequest) (*productservice.FindAllResponse, error) {
	return &productservice.FindAllResponse{Products: []*productservice.Product{
		&productservice.Product{
			Id:       "p01",
			Name:     "name 1",
			Price:    4.5,
			Quantity: 4,
			Status:   true,
		},
		&productservice.Product{
			Id:       "p02",
			Name:     "name 2",
			Price:    22,
			Quantity: 3,
			Status:   false,
		},
		&productservice.Product{
			Id:       "p03",
			Name:     "name 3",
			Price:    27,
			Quantity: 3,
			Status:   true,
		},
	}}, nil
}




In productservice folder, create new folder named server. In this folder, create new file named main.go as below:

package main

import (
	"fmt"
	"net"
	"productservice/handlers"
	productservice "productservice/proto"

	"google.golang.org/grpc"
)

func main() {

	lis, err := net.Listen("tcp", ":1111")
	if err != nil {
		fmt.Println(err)
	}
	defer lis.Close()

	productServ := handlers.ProductServiceServer{}
	grpcServer := grpc.NewServer()

	productservice.RegisterProductServiceServer(grpcServer, &productServ)

	if err := grpcServer.Serve(lis); err != nil {
		fmt.Println(err)
	}

}

Select main.go file in server folder and open it in Terminal window of Visual Studio Code. Use command line as below to start server:

go run main.go




In src folder, create new folder named client. In this folder, create new file named main.go as below:

package main

import (
	"context"
	"fmt"
	productservice "productservice/proto"

	"google.golang.org/grpc"
)

func main() {

	conn, err := grpc.Dial(
		"localhost:1111",
		grpc.WithInsecure(),
	)
	if err != nil {
		fmt.Println(err)
	}
	defer conn.Close()

	productServ := productservice.NewProductServiceClient(conn)

	response, err := productServ.FindAll(context.Background(), &productservice.FindAllRequest{})
	if err != nil {
		fmt.Println(err)
	} else {
		products := response.Products
		fmt.Println("Product List")
		for _, product := range products {
			fmt.Println("id: ", product.Id)
			fmt.Println("name: ", product.Name)
			fmt.Println("price: ", product.Price)
			fmt.Println("quantity: ", product.Quantity)
			fmt.Println("status: ", product.Status)
			fmt.Println("========================")
		}
	}

}

Select main.go file in client folder and open it in Terminal window of Visual Studio Code. Use command line as below to start client:

go run main.go




Product List
id:  p01
name:  name 1
price:  4.5
quantity:  4
status:  true
========================
id:  p02
name:  name 2
price:  22
quantity:  3
status:  false
========================
id:  p03
name:  name 3
price:  27
quantity:  3
status:  true
========================