云海的博客
首页
  • 接口
  • 数组
  • slice
  • map
  • 指针
  • 反射
  • Context
  • sync.map
  • 锁
  • 类型和类型指针分析
  • recover
  • 从零实现RPC框架
  • make和new区别
  • channel
  • sync.Once
  • sync.Pool
  • protobuf
  • MongoDB pkg源码-findone
  • MyBatis
  • Maven
  • 解析Laravel框架—路由处理
  • PHP(客户端)与 Golang(服务端)使用grpc+protobuf 通信
  • JAVA(客户端)与 Golang(服务端) 使用grpc+protobuf通信
  • Docker使用笔记-常用命令
  • Docker使用笔记-容器间通讯
  • Docker使用笔记-搭建Redis集群
  • Docker使用笔记-镜像多阶段构建
  • Kubernetes部署golang服务
  • Linux常用命令
  • Docker安装Prometheus与Grafana
  • Protobuf
  • TCP抓包
  • 概述-《TCP/IP详解》读书笔记
  • 索引
  • 事务隔离级别
  • 常识
  • 每日一题(1)
  • 每日一题(2)
  • 每日一题(3)
  • 每日一题(4)
关于
GitHub (opens new window)

云海

服务端研发
首页
  • 接口
  • 数组
  • slice
  • map
  • 指针
  • 反射
  • Context
  • sync.map
  • 锁
  • 类型和类型指针分析
  • recover
  • 从零实现RPC框架
  • make和new区别
  • channel
  • sync.Once
  • sync.Pool
  • protobuf
  • MongoDB pkg源码-findone
  • MyBatis
  • Maven
  • 解析Laravel框架—路由处理
  • PHP(客户端)与 Golang(服务端)使用grpc+protobuf 通信
  • JAVA(客户端)与 Golang(服务端) 使用grpc+protobuf通信
  • Docker使用笔记-常用命令
  • Docker使用笔记-容器间通讯
  • Docker使用笔记-搭建Redis集群
  • Docker使用笔记-镜像多阶段构建
  • Kubernetes部署golang服务
  • Linux常用命令
  • Docker安装Prometheus与Grafana
  • Protobuf
  • TCP抓包
  • 概述-《TCP/IP详解》读书笔记
  • 索引
  • 事务隔离级别
  • 常识
  • 每日一题(1)
  • 每日一题(2)
  • 每日一题(3)
  • 每日一题(4)
关于
GitHub (opens new window)
  • 接口
  • 数组
  • slice
  • map
  • 反射
  • sync.Pool
  • net包笔记
  • net-rpc分析
  • 指针
  • 数组排序
  • Context
  • sync.map
  • 锁
  • recover
  • 泛型
  • 类型和类型指针分析
  • make和new区别
  • channel
  • sync.Once
  • protobuf
    • GoLand debug(1)
    • 从零实现RPC框架
    • 从零开始学Go Origin框架
    • MongoDB pkg源码-findone
    • Golang
    云海
    2021-09-22
    目录

    protobuf

    # 概述

    protobuf 性能和效率大幅度优于JSON、XML等其它的结构化数据格式。

    # 示例

    syntax = "proto3";
    package main;
    
    message Student {
        string name = 1;
        bool male = 2;
        repeated int32 scores = 3;
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    • protobuf有两个版本,默认版本是proto2,如果需要proto3,则需要在非空非注释的第一行使用syntax="proto3"标明版本。
    • package,即包名声明符是可选的,用来防止不同的消息类型有命名冲突。
    • 消息类型使用message关键字定义,Student是类型名,name,male,scores是该类型的3个字段,类型分别为string,bool和[]int32。
    • repeated表示字段可重复,即用来表示Go语言中的数组类型。
    • 每个字符 = 后面的数字称为标识符,每个字段都需要提供一个唯一的标识符。
    • .proto文件可以写注释,单行注释//,多行注释/* */
    • 一个.proto文件中可以写多个消息类型,即对应多个结构体(struct)。
    • reserved可以在版本升级时使用。

    # 枚举

    枚举类型适用于提供一组预定义的值,选择其中一个。例如我们将性别定义为枚举类型。

    message Student {
        string name = 1;
        enum Gender {
            FEMALE = 0;
            MALE = 1;
        }
    
        Gender gender = 2;
        repeated int32 scores = 3;
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    # 定义服务

    service SearchService {
        rpc Search (SearchRequest) returns (SearchResponse);
    }
    
    1
    2
    3
    上次更新: 2023/01/11
    sync.Once
    GoLand debug(1)

    ← sync.Once GoLand debug(1)→

    最近更新
    01
    函数
    04-11
    02
    面试题
    04-11
    03
    EFK日志收集系统单机版
    08-18
    更多文章>
    Theme by Vdoing | Copyright © 2022-2025 Evan Xu | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式