美文网首页
proto文件编译

proto文件编译

作者: 大雄good | 来源:发表于2019-07-21 23:04 被阅读0次

    protobuf现在越来越流行,有时候需要重新加一条protobuf,平时都是抄别人的写法,但是有时候要加比较多的protobuf,想要快速验证结果,并且有没有语法问题。所以找了一下本地和docker编译protobuf的方法。

    1.在mac上安装编译环境

    安装:

    wget https://github.com/protocolbuffers/protobuf/releases/download/v3.9.0/protoc-3.9.0-osx-x86_64.zip
    unzip protoc-3.9.0-osx-x86_64.zip proto-3.9
    cp proto-3.9/bin/protoc $PATH
    

    上面命令敲完了,我们就可以编辑一个proto文件来测试一下,这里我使用的官方例子,首先创建了一个tutorial文件夹:

    mkdir tutorial
    

    在tutorial中创建一个proto文件addressbook.proto

    syntax = "proto3";
    package tutorial;
    message Person {
      string name = 1;
      int32 id = 2;  // Unique ID number for this person.
      string email = 3;
    
      enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
      }
    
      message PhoneNumber {
        string number = 1;
        PhoneType type = 2;
      }
    
      repeated PhoneNumber phones = 4;
    
    }
    
    // Our address book file is just one of these.
    message AddressBook {
      repeated Person people = 1;
    }
    

    执行编译命令:

    protoc -I=./tutorial --go_out=./tutorial ./tutorial/*.proto
    

    这样我们就能发现在tutorial文件夹下面成功创建另一个文件addressbook.pb.go:

    .
    └── tutorial
        ├── addressbook.pb.go
        └── addressbook.proto
    

    2.docker编译protobuf

    网上我发现znly/protoc这个docker image里面已经包含了protobuf编译的工具,因此我们直接执行下面的命令就可以编译tutorial下的proto文件:

    docker run --rm -v $(PWD):$(PWD) -w $(PWD) znly/protoc -I=./tutorial --go_out=./tutorial ./tutorial/*.proto
    

    总结

    这里简单记录了一下自己安装和编译protobuf的过程,可以发现有了docker这个工具,编译protobuf也是很简单的,另外关于protobuf的语法可以查阅官方文档

    相关文章

      网友评论

          本文标题:proto文件编译

          本文链接:https://www.haomeiwen.com/subject/sbrblctx.html