Home  >  Article  >  Backend Development  >  Detailed explanation of how golang uses protobuf

Detailed explanation of how golang uses protobuf

尚
forward
2020-01-14 17:52:453321browse

Detailed explanation of how golang uses protobuf

1. Download protobuf compiler protoc

Address: https://github.com/google/protobuf/releases

window:

Download: protoc-3.3.0-win32.zip

Unzip, copy protoc.exe in the bin directory to GOPATH/bin, and add GOPATH/bin to the environment variable.

Of course, it can also be placed in other directories. Environment variables need to be added to allow the system to find protoc.exe

linux:

Download: protoc-3.3.0-linux- Unzip x86_64.zip or protoc-3.3.0-linux-x86_32.zip

, copy the protoc in the bin directory to GOPATH/bin, and add GOPATH/bin to the environment variable.

If you like to compile and install, you can also download the source code and install it yourself, and finally add the executable file to the environment variable.

2. Get the protobuf compiler plug-in protoc-gen-go

Enter the GOPATH directory

Run

> go get -u github.com/golang/protobuf/protoc-gen-go

If successful, it will be in GOPATH/bin Generate the protoc-gen-go.exe file

3. Create a test.proto file

//指定版本
//注意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. Run the following command

> protoc --go_out=. *.proto

will generate a test.pb. go file

5. Use protobuf in go language

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();
}

Detailed explanation of how golang uses protobufFor more go language knowledge, please pay attention to the PHP Chinese websitego language tutorial column.

The above is the detailed content of Detailed explanation of how golang uses protobuf. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete