Build scalable microservices with Go and gRPC for high-performance communication
# Go Microservices with gRPC
Build high-performance microservices with Go and gRPC using Google Antigravity IDE. This guide covers service definitions, streaming, and production deployment patterns.
## Why Go with gRPC?
Go provides excellent concurrency primitives while gRPC enables efficient binary communication. Google Antigravity IDE's Gemini 3 engine suggests optimal service architectures.
## Protocol Buffer Definition
```protobuf
// proto/user.proto
syntax = "proto3";
package user;
option go_package = "github.com/myapp/proto/user";
service UserService {
rpc GetUser(GetUserRequest) returns (User);
rpc ListUsers(ListUsersRequest) returns (ListUsersResponse);
rpc CreateUser(CreateUserRequest) returns (User);
rpc UpdateUser(UpdateUserRequest) returns (User);
rpc DeleteUser(DeleteUserRequest) returns (Empty);
rpc StreamUsers(StreamUsersRequest) returns (stream User);
}
message User {
string id = 1;
string email = 2;
string name = 3;
string role = 4;
int64 created_at = 5;
int64 updated_at = 6;
}
message GetUserRequest {
string id = 1;
}
message ListUsersRequest {
int32 page = 1;
int32 limit = 2;
string search = 3;
}
message ListUsersResponse {
repeated User users = 1;
int32 total = 2;
int32 page = 3;
}
message CreateUserRequest {
string email = 1;
string name = 2;
string password = 3;
}
message UpdateUserRequest {
string id = 1;
optional string name = 2;
optional string email = 3;
}
message DeleteUserRequest {
string id = 1;
}
message Empty {}
```
## Server Implementation
```go
// internal/server/user_server.go
package server
import (
"context"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/myapp/proto/user"
)
type UserServer struct {
pb.UnimplementedUserServiceServer
repo UserRepository
}
func NewUserServer(repo UserRepository) *UserServer {
return &UserServer{repo: repo}
}
func (s *UserServer) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.User, error) {
user, err := s.repo.FindByID(ctx, req.Id)
if err != nil {
return nil, status.Error(codes.NotFound, "user not found")
}
return toProtoUser(user), nil
}
func (s *UserServer) ListUsers(ctx context.Context, req *pb.ListUsersRequest) (*pb.ListUsersResponse, error) {
users, total, err := s.repo.List(ctx, ListParams{
Page: int(req.Page),
Limit: int(req.Limit),
Search: req.Search,
})
if err != nil {
return nil, status.Error(codes.Internal, "failed to list users")
}
protoUsers := make([]*pb.User, len(users))
for i, u := range users {
protoUsers[i] = toProtoUser(u)
}
return &pb.ListUsersResponse{
Users: protoUsers,
Total: int32(total),
Page: req.Page,
}, nil
}
func (s *UserServer) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.User, error) {
user, err := s.repo.Create(ctx, CreateUserParams{
Email: req.Email,
Name: req.Name,
Password: req.Password,
})
if err != nil {
return nil, status.Error(codes.Internal, "failed to create user")
}
return toProtoUser(user), nil
}
func (s *UserServer) StreamUsers(req *pb.StreamUsersRequest, stream pb.UserService_StreamUsersServer) error {
users, err := s.repo.ListAll(stream.Context())
if err != nil {
return status.Error(codes.Internal, "failed to stream users")
}
for _, user := range users {
if err := stream.Send(toProtoUser(user)); err != nil {
return err
}
}
return nil
}
```
## Main Server Setup
```go
// cmd/server/main.go
package main
import (
"log"
"net"
"os"
"os/signal"
"syscall"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
pb "github.com/myapp/proto/user"
"github.com/myapp/internal/server"
)
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
opts := []grpc.ServerOption{
grpc.UnaryInterceptor(loggingInterceptor),
grpc.StreamInterceptor(streamLoggingInterceptor),
}
grpcServer := grpc.NewServer(opts...)
userServer := server.NewUserServer(repo)
pb.RegisterUserServiceServer(grpcServer, userServer)
reflection.Register(grpcServer)
go func() {
log.Printf("gRPC server listening on :50051")
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
grpcServer.GracefulStop()
}
```
## Best Practices
- Use protocol buffers for type-safe APIs
- Implement proper error codes
- Add interceptors for logging and auth
- Use streaming for large datasets
- Apply connection pooling
- Enable reflection for debugging
Google Antigravity IDE provides gRPC patterns and automatically generates Go code from your protocol buffer definitions.This Go prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their go implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For Go projects, consider mentioning your framework version, coding style, and any specific libraries you're using.