golang 使用protobuf
程序员文章站
2022-05-29 22:21:58
...
安装protobuf工具:
安装protobuf工具:
安装protoc:
$ apt install golang-github-gogo-protobuf-dev
安装protoc-gen-go:
$ apt install golang-goprotobuf-dev
protoc-gen-go是用来将protobuf的代码转换成go语言代码的一个插件
$ go get -u github.com/golang/protobuf/protoc-gen-go
proto是protobuf在golang中的接口模块,用于调用marshal、unmarshal
$ go get -u github.com/golang/protobuf/proto
编辑proto协议文件:
[email protected]:~/zzz# cat protos
syntax = "proto3";
package protos;
message Info {
uint32 UID = 1;
int32 Power = 3;
int64 StartedAt = 4;
double OutputMoney = 7;
}
[email protected]:~/zzz#
生成golang代码:
[email protected]:~/zzz# tree
.
├── protos
└── protos.pb.go
0 directories, 2 files
[email protected]:~/zzz#
[email protected]:~/zzz# cat protos.pb.go.pb.go
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: protos.pb.go
/*
Package protos is a generated protocol buffer package.
It is generated from these files:
protos.pb.go
It has these top-level messages:
Info
*/
package protos
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type Info struct {
UID uint32 `protobuf:"varint,1,opt,name=UID,json=uID" json:"UID,omitempty"`
Power int32 `protobuf:"varint,3,opt,name=Power,json=power" json:"Power,omitempty"`
StartedAt int64 `protobuf:"varint,4,opt,name=StartedAt,json=startedAt" json:"StartedAt,omitempty"`
OutputMoney float64 `protobuf:"fixed64,7,opt,name=OutputMoney,json=outputMoney" json:"OutputMoney,omitempty"`
}
func (m *Info) Reset() { *m = Info{} }
func (m *Info) String() string { return proto.CompactTextString(m) }
func (*Info) ProtoMessage() {}
func (*Info) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *Info) GetUID() uint32 {
if m != nil {
return m.UID
}
return 0
}
func (m *Info) GetPower() int32 {
if m != nil {
return m.Power
}
return 0
}
func (m *Info) GetStartedAt() int64 {
if m != nil {
return m.StartedAt
}
return 0
}
func (m *Info) GetOutputMoney() float64 {
if m != nil {
return m.OutputMoney
}
return 0
}
func init() {
proto.RegisterType((*Info)(nil), "protos.Info")
}
func init() { proto.RegisterFile("protos.pb.go", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 141 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x29, 0x28, 0xca, 0x2f,
0xc9, 0x2f, 0xd6, 0x2b, 0x48, 0xd2, 0x4b, 0xcf, 0x17, 0x62, 0x83, 0xf0, 0x94, 0xf2, 0xb8, 0x58,
0x3c, 0xf3, 0xd2, 0xf2, 0x85, 0x04, 0xb8, 0x98, 0x43, 0x3d, 0x5d, 0x24, 0x18, 0x15, 0x18, 0x35,
0x78, 0x83, 0x98, 0x4b, 0x3d, 0x5d, 0x84, 0x44, 0xb8, 0x58, 0x03, 0xf2, 0xcb, 0x53, 0x8b, 0x24,
0x98, 0x15, 0x18, 0x35, 0x58, 0x83, 0x58, 0x0b, 0x40, 0x1c, 0x21, 0x19, 0x2e, 0xce, 0xe0, 0x92,
0xc4, 0xa2, 0x92, 0xd4, 0x14, 0xc7, 0x12, 0x09, 0x16, 0x05, 0x46, 0x0d, 0xe6, 0x20, 0xce, 0x62,
0x98, 0x80, 0x90, 0x02, 0x17, 0xb7, 0x7f, 0x69, 0x49, 0x41, 0x69, 0x89, 0x6f, 0x7e, 0x5e, 0x6a,
0xa5, 0x04, 0xbb, 0x02, 0xa3, 0x06, 0x63, 0x10, 0x77, 0x3e, 0x42, 0x28, 0x09, 0x62, 0xaf, 0x31,
0x20, 0x00, 0x00, 0xff, 0xff, 0x6c, 0xb5, 0xbc, 0x83, 0x8e, 0x00, 0x00, 0x00,
}
[email protected]:~/zzz#
上一篇: golang 截取字符串
下一篇: kvm虚拟机管理
推荐阅读
-
省点花锦鲤卡app怎么激活 省点花锦鲤卡激活后怎么使用
-
Android自定义View 使用PathMeasure简单模仿系统ProgressBar(四)
-
solidworks零件模型怎么使用剖面命令?
-
Android studio怎么使用git获取最新内容然后合并?
-
省点花锦鲤卡可以在美团上使用吗 省点花锦鲤卡app怎么在美团上用
-
Wing FTP Server FTP服务器端中文版安装使用教程
-
eclipse格式化代码快捷键无法使用怎么办?
-
android使用DataBinding来设置空状态
-
IDPhotoStudio证件照打印使用教程
-
百中搜优化软件怎么样?百中搜优化软件使用教程(附视频教程)