Go Academy · Lesson

Implementing a Unary gRPC Service

Server and client with grpc-go

Lesson 2 of 413 steps

Implementing a Unary gRPC Service is a free Go Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

What is a unary RPC?

A unary RPC is the simplest gRPC pattern: the client sends one request and receives one response, similar to a regular function call over the network.

Server implementation

Implement the generated interface. Embed the Unimplemented*Server struct for forward compatibility:

type userServer struct {
    pb.UnimplementedUserServiceServer
    db *sql.DB
}

func (s *userServer) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.User, error) {
    // query DB
    return &pb.User{Id: req.Id, Name: "Alice"}, nil
}

Starting the server

Create a gRPC server, register the implementation, and listen on a TCP port:

lis, _ := net.Listen("tcp", ":50051")
grpcSrv := grpc.NewServer()
pb.RegisterUserServiceServer(grpcSrv, &userServer{})
log.Fatal(grpcSrv.Serve(lis))

Client connection

Dial the server and create a client stub:

conn, err := grpc.NewClient("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil { log.Fatal(err) }
defer conn.Close()
client := pb.NewUserServiceClient(conn)

Making a unary call

Call the RPC method on the client stub as if it were a local function:

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
user, err := client.GetUser(ctx, &pb.GetUserRequest{Id: 42})
if err != nil { log.Fatalf("error: %v", err) }
fmt.Println(user.Name)

Error handling

gRPC uses status codes. Return errors with status.Errorf and check them with status.Code(err):

import "google.golang.org/grpc/status"
import "google.golang.org/grpc/codes"

// Server:
return nil, status.Errorf(codes.NotFound, "user %d not found", req.Id)
// Client:
if status.Code(err) == codes.NotFound { /* handle */ }

Context propagation

The context passed to the client RPC call carries deadlines and cancellation to the server. The server should pass it to downstream calls (DB, other RPCs).

TLS for production

Replace insecure credentials with TLS in production:

creds, _ := credentials.NewServerTLSFromFile("cert.pem", "key.pem")
grpcSrv := grpc.NewServer(grpc.Creds(creds))

UnimplementedServer

Embedding pb.UnimplementedXxxServer provides stub implementations of all RPC methods that return Unimplemented status. This ensures the server compiles when new RPCs are added to the proto before you implement them.

grpc.Dial vs grpc.NewClient

grpc.Dial is deprecated in newer versions. Use grpc.NewClient which does not establish a connection eagerly but lazily on the first RPC call.

Reflection service

Register the gRPC reflection service to allow tools like grpcurl to introspect available services without the .proto file:

reflection.Register(grpcSrv)

Quick Check

What is the purpose of embedding pb.UnimplementedXxxServer in a gRPC server struct?

Recap: Unary gRPC

Key points:

  • Implement generated interface; embed UnimplementedServer
  • grpc.NewServer() + Register + Serve on TCP listener
  • Client: grpc.NewClient + generated NewXxxClient
  • Use status.Errorf/status.Code for gRPC error handling
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Implementing a Unary gRPC Service” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Implementing a Unary gRPC Service”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

What will I learn in “Implementing a Unary gRPC Service”?

Server and client with grpc-go You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Implementing a Unary gRPC Service” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Protocol Buffers and .proto Files
  2. Implementing a Unary gRPC Service
  3. Server and Client Streaming
  4. gRPC Interceptors and Metadata
← Back to Go Academy