一、Go安装及使用protobuf工具

以下都是基于Linux系统;

1.安装

A) protobuf 编译工具安装

1、下载 protoBuf:

cd $GOPATH/src/
git clone https://github.com/protocolbuffers/protobuf.git

2、或者直接将压缩包拖入后解压

unzip protobuf.zip

3、安装依赖库

sudo apt-get install autoconf  automake  libtool curl make  g++  unzip libffi-dev -y

4、进入目录

cd protobuf/

5、自动生成configure配置文件:

./autogen.sh

6、配置环境:

./configure

7、编译源代码(时间比较长):

make

8、安装

sudo make install

9、刷新共享库 (很重要的一步啊)

sudo ldconfig

10、成功后需要使用命令测试

protoc -h
B) protobuf 的 go 语言插件安装

由于protobuf并没直接支持go语言需要我们手动安装相关插件

  1. 获取 proto包(Go语言的proto API接口)
go get  -v -u github.com/golang/protobuf/proto
go get -v -u github.com/golang/protobuf/protoc-gen-go

​ 2.编译

cd $GOPATH/src/github.com/golang/protobuf/protoc-gen-go/
go build

​ 3.将生成的 protoc-gen-go可执行文件,放在/bin目录下

sudo cp protoc-gen-go /bin/

tips:上面第2步报错,由于go install可以直接安装protobuf了,跟错误提示安装;

go install google.golang.org/protobuf@latest

2.使用

pb语法就不介绍了;

.proto文件:

syntax="proto3";                //Proto协议
package pb; //当前包名
option csharp_namespace="Pb"; //给C#提供的选项
option go_package = "./"; //输出路径 message BroadCast{
int32 PID =1;
int32 Tp = 2;
oneof Data{
string Content = 3;
}
}

直接执行以下命令,该文件夹下.proto文件都会被编译成go文件;

protoc --go_out=.  *.proto

Tips:我这里生成的go文件包名都是__,知道哪里出了错,手动改一下就完了;

具体使用方法:

package main

import (
"fmt"
"gameserver/pb" "google.golang.org/protobuf/proto"
) func main() {
data := &pb.BroadCast{
PID: 1,
Tp: 2,
Data: &pb.BroadCast_Content{
Content: "hello protobuf!perilla",
},
}
//序列化
binaryData, err := proto.Marshal(data)
if err != nil {
fmt.Println("marshal err:", err)
} newdata := &pb.BroadCast{}
//反序列化
err = proto.Unmarshal(binaryData, newdata)
if err != nil {
fmt.Println("unmarshal err:",err)
} fmt.Println(newdata)
}

二、C#使用protobuf工具

C#就不用geogle的protobuf了,用geogle另一个protobuf-net;

下载地址

工程有重复dll,只留一个unity文件中的dll;

类加[ProtoBuf.ProtoContract]特性,类中字段 [ProtoBuf.ProtoMember(1)];

[ProtoBuf.ProtoContract]
public class Porperties
{
[ProtoBuf.ProtoMember(1)]
public int id;
[ProtoBuf.ProtoMember(2)]
public string name;
[ProtoBuf.ProtoMember(3)]
public List<int> attributes; public void Init(int id, string name, List<int> attributes)
{
this.id = id;
this.name = name;
this.attributes = attributes;
}
} void Start()
{
Porperties p = new Porperties();
int[] arr = { 1, 33, 44, 63 }; p.Init(1, "perilla", new List<int>(arr));
using (FileStream stream = File.OpenWrite("test.dat"))
{
//序列化
ProtoBuf.Serializer.Serialize<Porperties>(stream, p);
} Porperties p2 = new Porperties(); using (FileStream stream = File.OpenRead("test.dat"))
{
//从文件中读取数据并反序列化
p2 = ProtoBuf.Serializer.Deserialize<Porperties>(stream);
}
Debug.Log(string.Format(p2.name,p2.id,p2.attributes[2]));
}

Protobuf使用--go和C#的更多相关文章

  1. python通过protobuf实现rpc

    由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc.rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行g ...

  2. Protobuf使用规范分享

    一.Protobuf 的优点 Protobuf 有如 XML,不过它更小.更快.也更简单.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍.你可以定义自己的数据结构 ...

  3. java netty socket库和自定义C#socket库利用protobuf进行通信完整实例

    之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...

  4. 在Wcf中应用ProtoBuf替代默认的序列化器

    Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...

  5. protobuf的编译安装

    github地址:https://github.com/google/protobuf支持多种语言,有多个语言的版本,本文采用的是在centos7下编译源码进行安装. github上有详细的安装说明: ...

  6. 编译protobuf的jar文件

    1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...

  7. protobuf学习(2)-相关学习资料

    protobuf官方git地址 protobuf官方英文文档   (你懂的需要FQ) protobuf中文翻译文档 protobuf概述          (官方翻译 推荐阅读) protobuf入门 ...

  8. google protobuf安装与使用

    google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...

  9. c# (ENUM)枚举组合类型的谷歌序列化Protobuf

    c# (ENUM)枚举组合类型的谷歌序列化Protobuf,必须在序列化/反序列化时加上下面: RuntimeTypeModel.Default[typeof(Alarm)].EnumPassthru ...

  10. dubbox 增加google-gprc/protobuf支持

    好久没写东西了,今年实在太忙,基本都在搞业务开发,晚上来补一篇,作为今年的收官博客.google-rpc 正式发布以来,受到了不少人的关注,这么知名的rpc框架,不集成到dubbox中有点说不过去. ...

随机推荐

  1. 为什么众多软件厂商无法提供APS高级计划排程系统?工厂目前生产计划是怎么排产的?

    一.行业现状如想了解一下目前现状,去考察一下上了ERP的企业,会发现一个有趣的现象该企业无论ERP软件搞得如何如火如荼,似乎都与生产调度人员无关. 车间里或者生产线上的生产作业计划.生产过程的调度和管 ...

  2. odoo views中html的奇怪问题

    在我创建了字段类型为 fields.Html 以后,确出现了两种不同的情况 下图中,content是此类型的,可以正常显示不需要加widget(小部件)="html" <fo ...

  3. Razor语法2

    语法名称 Razor 语法 Web Forms 等效语法 代码块 @{ int x = 123; string y = "because."; } <% int x = 12 ...

  4. github访问慢处理办法

    Windows 系统:C:\Windows\System32\drivers\etc\hostsLinux 系统:/etc/hostsMac(苹果电脑)系统:/etc/hostsAndroid(安卓) ...

  5. Spring Boot新增一个YML配置文件,并进行加载

    我们在同级目录下增加 然后增加一个配置类 SpringBootConfiguration.java import org.springframework.beans.factory.config.Ya ...

  6. SpringBoot 整合Spring Security框架

    引入maven依赖 <!-- 放入spring security依赖 --> <dependency> <groupId>org.springframework.b ...

  7. C++代码优化之nim_duilib控件关联

    前言 使用nim_duilib关联控件,优化后.的确减少了代码量,那么,执行效率呢? 时间执行的长短与硬件.软件有关,以下内容仅为参考. 测试代码 下面的代码,分为两部分, 循环使用for,非循环则直 ...

  8. 【LeetCode】1403. 非递增顺序的最小子序列 Minimum Subsequence in Non-Increasing Order

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcode ...

  9. 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  10. 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...