GRPC – Template

To install latest version of Protoc

sudo apt-get remove protobuf-compiler


curl -LO $PB_REL/download/v3.19.1/

For linux

sudo apt install unzip

sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc

sudo unzip -o $PROTOC_ZIP -d /usr/local include/*

chmod +x /usr/local/bin/protoc


We have used go mod name GRPC-SERVE (You can replace mod name by replacing in source code usually called most in import)

For updating go mod please check go documentation. Basic command here to start from new

go mod init GRPC-SERVE

Go install require package mentioned in tools.go Details instruction at :

go mod tidy

go install \[email protected] \[email protected] \[email protected] \[email protected]

Generate compiled go code for grpc gateway

make file_name=home
here (home) is the proto name

HTTP and RPC server configuration

Update config/config.yaml file

Start gRPC Server

go run server/main.go

start reverse proxe server

go run reverse-proxy/main.go

localhost url

ENDPOINT: http://localhost:port/v1/loan/homedata?value=test

Curl check

curl -d "value=test data again" http://localhost:port/v1/loan/homedata?value=test

Run SwaggerUI :-

go to the swaggerui dir and run cmd
go run main.go
once server starts go to the browser and hit 

GoDoc :-

GoDoc Creation

Example :: creating go doc for file.go

// Package <PackageName> <purpose of package>

package <PackageName>

// Function <FunctionName> <Function type>
// Request *PackageRequest as request and 
// Response *PackageResponse, error

func (s *PackageStruct) ReceiverFunctionName(ctx context.Context, req *PackageRequest) (*PackageResponse, error) {

    return &PackageResponse, nil

running godoc

godoc -http=:6060

make sure port 6060 is free or use the suitable port

open browser


!! you will get all your docs here !!

Zap (Level logging) :-

For Development Environment

logger, _ := zap.NewDevelopment()

Debug only works in development environment

For Production Environment

logger, _ := zap.NewProduction() -->


helper.SugarObj.Info("Info Level")

Testing :-

• To write unit tests in GoLang, we need to import the testing package. There are some rules when doing testing. The filename must end with _test.


• The command to be used is go test in the CLI. It will test all the files set up as test files and return the output in the terminal.

• The functions in that test file must follow this signature.

    Func TestName(t *Testing.T){
    //code for testing

• Try running-

   go test -cover

You should see-

coverage: 100.0% of statements


• Function

    func BenchmarkXxx(*testing.B){
       //code for testing/benchmark.

• Benchmarks are executed by the go test command when its -bench flag is provided. Benchmarks are run sequentially.

• A sample Benchmark function look like this-

    func BenchmarkRandInt(b *testing.B) {
        for i := 0; i < b.N; i++ {

• If Benchmark need some expensive settings , the timer must be reset;-


• When running the go test we must provide the -bench flag which will show the output.

    // the dot is the regex matching everything
    go test -bench=.    

• If a benchmark needs to test performance in a parallel setting, it may use the RunParallel helper function; such benchmarks are intended to be used with the

    go test -cpu flag:


• Tests or benchmarks may be skipped at run time with a call to the Skip method of *T or *B:

• eg:-

func TestTimeConsuming(t *testing.T) {
    if testing.Short() {
        t.Skip("skipping test in short mode.")


View Github