zoukankan      html  css  js  c++  java
  • Go使用protobuf

    本文更新于2019-01-03。

    1. https://github.com/protocolbuffers/protobuf/releases下载protoc(如:Windows则下载protoc-3.6.1-win32.zip)。protoc命令位于bin目录下。

    2. go get github.com/golang/protobuf

    3. 编译github.com/golang/protobuf/protoc-gen-go。因protoc需调用protoc-gen-go,故需将protoc-gen-go放在环境变量PATH指定的目录中,或protoc所在的目录。

    4. 定义proto文件。如:

      syntax = "proto2";
      package example;
      
      enum FOO { X = 17; };
      
      message Test {
        required string label = 1;
        optional int32 type = 2 [default=77];
        repeated int64 reps = 3;
      }
      
    5. 使用protoc生成go代码,生成的文件名为*.pb.go。

      protoc --proto_path=IMPORT_PATH --go_out=DST_DIR *.proto
      
      • --proto_path:同-I,指定proto文件的目录,缺省则为当前进程目录。
      • --go_out:指定go文件生成目录。
    6. 调用(示例中假设生成的go代码位于path/to/example)。

      package main
      
      import (
      	"log"
      	
      	"github.com/golang/protobuf/proto"
      	
      	"path/to/example"
      )
      
      func main() {
      	test := &example.Test{
      		Label: proto.String("hello"),
      		Type:  proto.Int32(17),
      		Reps:  []int64{1, 2, 3},
      	}
      	data, err := proto.Marshal(test)
      	if err != nil {
      		log.Fatal("marshaling error: ", err)
      	}
      	newTest := &example.Test{}
      	err = proto.Unmarshal(data, newTest)
      	if err != nil {
      		log.Fatal("unmarshaling error: ", err)
      	}
      	// Now test and newTest contain the same data.
      	if test.GetLabel() != newTest.GetLabel() {
      		log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
      	}
      	// etc.
      }
      
  • 相关阅读:
    js 与或运算符 || && 妙用(great!!!)
    type of && undefined
    全新框架?微信小程序与React Native的异同之处
    JS-十五章(15.16)
    JS-第十三章
    ValueError: zero-size array to reduction operation maximum which has no identity
    time
    模块
    day8-函数
    day7-format字符串格式化
  • 原文地址:https://www.cnblogs.com/garvenc/p/use_protobuf_in_go.html
Copyright © 2011-2022 走看看