codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

49 / 72 topics
42Third-Party Libraries Overview43GORM (Object-Relational Mapping)44Gin Framework45Echo Framework46Cobra CLI Library47Viper Config Library48Go Bindata for Embedding Assets49gRPC (Remote Procedure Calls)50Protocol Buffers
Tutorials/Go (Golang)/gRPC (Remote Procedure Calls)
🐹Go (Golang)

gRPC (Remote Procedure Calls)

Updated 2026-04-20
2 min read

Introduction

gRPC is a high-performance, open-source universal RPC framework developed by Google. It uses Protocol Buffers as its interface definition language (IDL) and supports multiple languages including Go (Golang). This tutorial will guide you through setting up gRPC in a Go application, defining services, generating client and server code, and implementing best practices.

Prerequisites

Before diving into gRPC, ensure you have the following installed:

  • Go (Golang) 1.16 or later
  • Protocol Buffers compiler (protoc)
  • Go plugin for protoc

You can install the Go plugin for protoc using:

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest

Setting Up Your Project

Create a new directory for your gRPC project and initialize it with Go modules:

mkdir grpc-tutorial
cd grpc-tutorial
go mod init grpc-tutorial

Defining the Service

gRPC uses Protocol Buffers to define services. Create a file named service.proto in your project directory:

syntax = "proto3";

package tutorial;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

Generating Go Code

Use protoc to generate Go code from your .proto file:

protoc --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    service.proto

This command generates two files: service.pb.go and service_grpc.pb.go. These files contain the necessary code for defining your gRPC service, messages, and client/server stubs.

Implementing the Server

Create a file named server.go to implement the server:

package main

import (
	"context"
	"log"
	"net"

	pb "grpc-tutorial/tutorial"
	"google.golang.org/grpc"
)

type server struct {
	pb.UnimplementedGreeterServer
}

func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
	log.Printf("Received: %v", in.GetName())
	return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}

func main() {
	lis, err := net.Listen("tcp", ":50051")
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	log.Printf("server listening at %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}

Implementing the Client

Create a file named client.go to implement the client:

package main

import (
	"context"
	"log"
	"time"

	pb "grpc-tutorial/tutorial"
	"google.golang.org/grpc"
)

const (
	address     = "localhost:50051"
	defaultName = "world"
)

func main() {
	conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewGreeterClient(conn)

	name := defaultName
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Greeting: %s", r.GetMessage())
}

Running the Application

  1. Start the server:
go run server.go
  1. Run the client in another terminal window:
go run client.go

You should see the following output in the client's terminal:

Greeting: Hello world

And in the server's terminal:

Received: world
server listening at [::]:50051

Best Practices

  • Use Protocol Buffers for Data Serialization: gRPC uses Protocol Buffers, which are efficient and language-neutral. This ensures that your service can be easily consumed by clients written in different languages.

  • Define Services Carefully: Clearly define the services and messages in your .proto file. This helps in maintaining a clean and understandable API contract.

  • Error Handling: Implement proper error handling in both client and server code. Use gRPC's status codes to convey meaningful errors to clients.

  • Security: Always secure your gRPC services using TLS/SSL for production environments. You can use grpc.WithTransportCredentials to enable transport security.

  • Concurrency Management: Be mindful of concurrency when implementing your gRPC service methods. Use Go's goroutines and channels effectively to handle concurrent requests.

Conclusion

gRPC is a powerful tool for building efficient, high-performance RPC services in Go. By following this tutorial, you have learned how to define services using Protocol Buffers, generate client and server code, and implement both the server and client sides of a gRPC application. With these skills, you can build robust and scalable microservices architectures.

For more advanced topics, consider exploring features like streaming RPCs, interceptors, and service discovery in gRPC.


PreviousGo Bindata for Embedding AssetsNext Protocol Buffers

Recommended Gear

Go Bindata for Embedding AssetsProtocol Buffers