Hands‑On Guide to Building and Testing gRPC Services with Go

This article walks through setting up the Go environment, defining protobuf files, generating code, and implementing both server and client sides of a gRPC service, complete with runnable examples, common pitfalls, and output verification to help developers quickly adopt Go‑based gRPC testing.

FunTester
FunTester
FunTester
Hands‑On Guide to Building and Testing gRPC Services with Go

The author revisits a previous Java gRPC tutorial and shares a practical, step‑by‑step experience of developing and testing gRPC services using Go, noting that the Go toolchain is simpler but the ecosystem is less mature.

Prerequisites

Install the Go language runtime and an IDE such as GoLand.

Install protoc-gen-go or the higher‑performance protoc-gen-gofast plugin.

Install the gRPC library; network issues may require manual resolution.

Generate the demo code with the appropriate protoc command and flags.

Proto Definition

The protobuf file is almost identical to the Java version, with a slight difference in the package option.

syntax = "proto3";

option go_package = "./;proto";

service HelloService {
  rpc ExecuteHi (HelloRequest) returns (HelloResponse) {};
}

message HelloRequest {
  string name = 1;
  int32 age = 2;
}

message HelloResponse {
  string msg = 1;
}

Pay attention to the go_package option; older tutorials may use different syntax, so verify the current documentation.

Server Implementation

The Go server can be placed in the same directory as the .proto file.

type Ser struct{}

func (s *Ser) ExecuteHi(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
    return &pb.HelloResponse{Msg: "Hello " + in.Name}, nil
}

func TestGrpcService(t *testing.T) {
    // Listen on 127.0.0.1:12345
    lis, err := net.Listen("tcp", "127.0.0.1:12345")
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    // Create gRPC server
    s := grpc.NewServer()
    pb.RegisterHelloServiceServer(s, &Ser{})
    // Register reflection service
    reflection.Register(s)
    // Start serving
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

If pb.RegisterHelloServiceServer(s, &Ser{}) fails, check the versions of protoc-gen-go or protoc-gen-gofast and the error message for clues.

Client Implementation

The client demonstrates a simple unary call; the author notes uncertainty about streaming parameters.

func TestGrpcClient(t *testing.T) {
    conn, _ := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
    defer conn.Close()
    c := pb.NewHelloServiceClient(conn)
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    defer cancel()
    r, _ := c.ExecuteHi(ctx, &pb.HelloRequest{Name: "FunTester", Age: 23})
    fmt.Printf("msg: %s
", r.Msg)
}

Running Results

Server output (no explicit log statements): 忘记打日志了。没有输出 Client output:

=== RUN   TestGrpcClient
msg: Hello FunTester
--- PASS: TestGrpcClient (0.00s)
PASS

Conclusion

The author completed the Go gRPC testing practice but does not plan to use Go as the primary gRPC testing language at work, preferring to continue with Java for future testing projects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendtestingGogRPCProtobufTutorial
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.