golang gen pb文件

290 阅读2分钟

在 Go 项目中使用 Protocol Buffers(protobuf)是一种常见的序列化数据的方式。为了生成 .pb.go 文件,你需要安装 Protocol Buffers 编译器和 Go 的插件 protoc-gen-go

安装 Protocol Buffers 编译器

  1. 下载并安装 Protocol Buffers 编译器(protoc)。

    • Protocol Buffers releases 页面下载对应操作系统的编译器。
    • 解压下载的文件,并将 protoc 可执行文件添加到系统的 PATH 环境变量中。

    例如,在 Linux 或 macOS 上,可以通过以下步骤安装:

    # 下载 Protocol Buffers 编译器
    curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v21.3/protoc-21.3-linux-x86_64.zip
    
    # 解压文件
    unzip protoc-21.3-linux-x86_64.zip -d protoc
    
    # 将 protoc 可执行文件移动到系统 PATH 中,例如 /usr/local/bin
    sudo mv protoc/bin/protoc /usr/local/bin/
    
    # 确认安装成功
    protoc --version
    

安装 Go 插件

  1. 安装 protoc-gen-go 插件:

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

    安装 protoc-gen-go-grpc 插件(如果需要 gRPC 支持):

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

    确保 GOPATH/bin 目录在系统的 PATH 环境变量中:

    export PATH="$PATH:$(go env GOPATH)/bin"
    

定义 .proto 文件

  1. 创建一个 .proto 文件并定义消息和服务。例如,创建 example.proto 文件:
    syntax = "proto3";
    
    package example;
    
    // 定义一个简单的消息类型
    message HelloRequest {
        string name = 1;
    }
    
    message HelloResponse {
        string message = 1;
    }
    
    // 定义一个服务
    service Greeter {
        rpc SayHello (HelloRequest) returns (HelloResponse);
    }
    

生成 Go 代码

  1. 运行 protoc 命令生成 .pb.go 文件:

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

    这将生成两个文件:

    • example.pb.go:包含消息类型和基础的序列化、反序列化代码。
    • example_grpc.pb.go:包含 gRPC 服务接口代码。

示例项目结构

项目目录结构如下:

project/
├── example.proto
├── example.pb.go
├── example_grpc.pb.go
├── go.mod
└── main.go

示例代码

example.proto

syntax = "proto3";

package example;

message HelloRequest {
    string name = 1;
}

message HelloResponse {
    string message = 1;
}

service Greeter {
    rpc SayHello (HelloRequest) returns (HelloResponse);
}

main.go

package main

import (
    "context"
    "log"
    "net"

    "google.golang.org/grpc"
    pb "path/to/your/project"

    "google.golang.org/grpc/reflection"
)

const (
    port = ":50051"
)

type server struct {
    pb.UnimplementedGreeterServer
}

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

func main() {
    lis, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    s := grpc.NewServer()
    pb.RegisterGreeterServer(s, &server{})

    // Register reflection service on gRPC server.
    reflection.Register(s)
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

运行 gRPC 服务器

编写并运行 gRPC 服务器:

go run main.go

总结

通过以上步骤,您可以成功生成 Protocol Buffers 的 .pb.go 文件,并在 Go 项目中使用它们。包括安装 Protocol Buffers 编译器和 Go 插件、定义 .proto 文件、生成 Go 代码、以及使用生成的代码来实现 gRPC 服务器。