proto 基础

option

  • option go_package = "goGinT/hello/proto/v1;test";
    • go_package 生成go语言的代码
    • goGinT/hello/proto/v1 代表生成文件的目录
    • test 生成的包名

import

  • import "xxx.proto" 引入另一个 公用的 proto的内容

嵌套的 message、enum、map、Timestamp

定义

syntax = "proto3";
package test.v1;
import "google/protobuf/timestamp.proto";
option go_package = "goGinT/hello/proto/v1;test";

enum SexType {
  MALE = 0;
  FEMALE = 1;
}

message Hello {
  string name = 1;

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

  Result data = 2;
  SexType sex = 3;
  map <string, string> mapObj = 4;
  google.protobuf.Timestamp createTime = 5;
}

使用

package main

import (
	"fmt"
	test "goGinT/rpc/hello/protoc"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)

func main() {
	helloInfo := test.Hello{
		Name:       "大壮",
		Data:       &test.Hello_Result{Name: "里斯", Age: 12},
		Sex:        test.SexType_FEMALE,
		MapObj:     map[string]string{"title": "唐诗", "age": "30"},
		CreateTime: timestamppb.Now(),
	}

	fmt.Println(&helloInfo)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值