0Pricing
Go Academy · Lesson

Implementing a Unary gRPC Service

Server and client with grpc-go

Implementing a Unary gRPC Service is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

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

Yes — the full text of “Implementing a Unary gRPC Service” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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