内容简介:1、下载protobuf的编译器protoc地址:window:
1、下载protobuf的编译器protoc
地址:
https://github.com/google/protobuf/releases
window:
下载: protoc-3.3.0-win32.zip
解压,把bin目录下的protoc.exe复制到GOPATH/bin下,GOPATH/bin加入环境变量。
当然也可放在其他目录,需加入环境变量,能让系统找到protoc.exe
linux:
下载:protoc-3.3.0-linux-x86_64.zip 或 protoc-3.3.0-linux-x86_32.zip
解压,把bin目录下的protoc复制到GOPATH/bin下,GOPATH/bin加入环境变量。
如果喜欢编译安装的,也可下载源码自行安装,最后将可执行文件加入环境变量。
2、获取protobuf的编译器插件protoc-gen-go
进入GOPATH目录
运行
go get -u github.com/golang/protobuf/protoc-gen-go
如果成功,会在GOPATH/bin下生成protoc-gen-go.exe文件
3、创建一个test.proto文件
//指定版本 //注意proto3与proto2的写法有些不同 syntax = "proto3"; //包名,通过protoc生成时 go 文件时 package test; //手机类型 //枚举类型第一个字段必须为0 enum PhoneType { HOME = 0; WORK = 1; } //手机 message Phone { PhoneType type = 1; string number = 2; } //人 message Person { //后面的数字表示标识号 int32 id = 1; string name = 2; //repeated表示可重复 //可以有多个手机 repeated Phone phones = 3; } //联系簿 message ContactBook { repeated Person persons = 1; }
4、运行如下命令
> protoc --go_out=. *.proto
会生成一个test.pb.go的文件,具体的文件内容我就不截图了。
5、在go语言中使用protobuf
package main;
import (
"github.com/golang/protobuf/proto"
"protobuf/test"
"io/ioutil"
"os"
"fmt"
)
func write() {
p1 := &test.Person{
Id: 1,
Name: "小张",
Phones: []*test.Phone{
{test.PhoneType_HOME, "111111111"},
{test.PhoneType_WORK, "222222222"},
},
};
p2 := &test.Person{
Id: 2,
Name: "小王",
Phones: []*test.Phone{
{test.PhoneType_HOME, "333333333"},
{test.PhoneType_WORK, "444444444"},
},
};
//创建地址簿
book := &test.ContactBook{};
book.Persons = append(book.Persons, p1);
book.Persons = append(book.Persons, p2);
//编码数据
data, _ := proto.Marshal(book);
//把数据写入文件
ioutil.WriteFile("./test.txt", data, os.ModePerm);
}
func read() {
//读取文件数据
data, _ := ioutil.ReadFile("./test.txt");
book := &test.ContactBook{};
//解码数据
proto.Unmarshal(data, book);
for _, v := range book.Persons {
fmt.Println(v.Id, v.Name);
for _, vv := range v.Phones {
fmt.Println(vv.Type, vv.Number);
}
}
}
func main() {
write();
read();
}
原文地址:https://www.cnblogs.com/jkko123/p/7161843.html
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Introduction to Computation and Programming Using Python
John V. Guttag / The MIT Press / 2013-7 / USD 25.00
This book introduces students with little or no prior programming experience to the art of computational problem solving using Python and various Python libraries, including PyLab. It provides student......一起来看看 《Introduction to Computation and Programming Using Python》 这本书的介绍吧!