golang 使用protobuf

golang 使用protobuf,第1张

安装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协议文件:
root@node1:~/zzz# cat protos
syntax = "proto3";

package protos;

message Info {
  uint32 UID = 1;
  int32 Power = 3;
  int64 StartedAt = 4;
  double OutputMoney = 7;
}
root@node1:~/zzz#
生成golang代码:
root@node1:~/zzz# tree
.
├── protos
└── protos.pb.go

0 directories, 2 files
root@node1:~/zzz# 
root@node1:~/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,
}
root@node1:~/zzz# 
test代码:
root@node1:~/zzz# tree
.
├── go.mod
├── go.sum
├── main.go
├── protos
│   ├── protos
│   └── protos.pb.go
└── test

1 directory, 6 files
root@node1:~/zzz# 
package main
  
import (
        "fmt"
        "test/protos"

        "github.com/golang/protobuf/proto"
        //"github.com/gogo/protobuf/proto"
)

func main() {
        var obj = &protos.Info{}
        obj.UID = 10
        obj.Power = 20
        obj.StartedAt = 30
        obj.OutputMoney = 40

        d, err := proto.Marshal(obj)
        if err != nil {
                fmt.Println(err)
                return
        }
        fmt.Println(d)

        var obj1 = &protos.Info{}
        err = proto.Unmarshal(d, obj1)
        if err == nil {
                fmt.Println(obj1)
        }
}
测试结果:
root@node1:~/zzz# go build
root@node1:~/zzz# ./test 
[8 10 24 20 32 30 57 0 0 0 0 0 0 68 64]
UID:10 Power:20 StartedAt:30 OutputMoney:40 
root@node1:~/zzz#

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/995400.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存