Installation and use of GOPROTOBUF under Windows

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. Installation

Read a lot of tutorials on the Internet, all mentioned to install Protoc and protoc-gen-go, but after trying to install the PROTOC is not correct, the record can be successfully installed PROTOC and Protoc-gen-go method. The premise is that go is already installed by default.

    • Installing PROTOC

      • Download the protoc-3.3.0-win32.zip package under this link
      • Unzip a file to a folder
      • Add the/bin/protoc.exe binary path of the extracted folder to the environment variable
    • Installing Protoc-gen-go

      • Directly in the terminal go get -u github.com/golang/protobuf/protoc-gen-go , you can %GOPATH/bin find a protoc-gen-go.exe under your path (this step cannot be completed, consider opening a global upside down wall)

Protoc and Protoc-gen-go have been completed.

2. Use

The cmd in Windows failed to generate the *.pb.go file through the command line, which was generated successfully in Git bash.

Create a new Test.proto file

package tutorial;  message Person {    required string name = 1;    required int32 age = 2;    optional string email = 3;    }  

Go to the folder where the Test.proto file is located and execute it in Git bash
protoc --plugin=protoc-gen-go=/f/gopath/bin/protoc-gen-go.exe --go_out=./ test.protoWhere--go_out is followed by the path of the output file and the path of the input proto file, but the following error occurs after execution


Error

According to the error, we know that the first line of the. proto file needs to be added one line of version notes, as follows

syntax = "proto2";package tutorial;    message Person {    required string name = 1;    required int32 age = 2;    optional string email = 3;    }    }    

Run the above command line again to get the file in the directory you specified test.pb.go , the file content is as follows:

Code generated by Protoc-gen-go. Do not edit.//Source:test.proto/*package tutorial are a generated protocol buffer package. It is generated from these files:test.protoIt have these top-level messages:person*/package tutorialimport proto "G Ithub.com/golang/protobuf/proto "Import FMT" FMT "Import Math" math "//Reference imports to suppress errors if they is not otherwise used.var _ = Proto. Marshalvar _ = fmt. Errorfvar _ = Math. inf//This was a compile-time assertion to ensure that this generated file//was compatible with the proto package it is Bei Ng compiled against.//A compilation error At the This line likely means your copy of the//proto package needs to be updated. Const _ = Proto. ProtoPackageIsVersion2//Upgrade the proto PackageType person struct {Name *string ' protobuf: ' byte S,1,req,name=name "JSON:" Name,omitempty "' Age *int32 ' protobuf:" Varint,2,req,name=age "JSON:" Age,omitempt Y "' Email *string ' protobuf:" Bytes,3,opt,namE=email "JSON:" Email,omitempty "' xxx_unrecognized []byte ' JSON:"-"'}func (M *person) Reset () {*m = person{}}func (M *person) string () string {return Proto. Compacttextstring (M)}func (*person) protomessage () {}func (*person) descriptor () ([]byte, []int) {return F IleDescriptor0, []int{0}}func (M *person) GetName () string {if M! = Nil && M.name! = Nil {return *M.N     AME} return ""}func (M *person) Getage () int32 {if m! = Nil && M.age! = nil {return *m.age}    Return 0}func (M *person) Getemail () string {if M! = Nil && M.email! = nil {return *m.email} Return ""}func init () {proto. Registertype ((*person) (nil), "tutorial. Person ")}func init () {proto. Registerfile ("Test.proto", fileDescriptor0)}var fileDescriptor0 = []byte{//106 bytes of a gzipped Filedescriptorprot o 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xFF, 0xe2, 0xe2, 0x2a, 0x49, 0x2d,0x2e, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x28, 0x29, 0x2d, 0xc9, 0x2f, 0xca, 0x4c, 0XCC, 0x51, 0x72, 0x E1, 0x62, 0x0b, 0x48, 0x2d, 0x2a, 0xce, 0XCF, 0x13, 0x12, 0xe2, 0x62, 0xc9, 0x4b, 0XCC, 0x4d, 0x95, 0x60, 0x54, 0x60, 0 Xd2, 0xe0, 0x0c, 0x02, 0xb3, 0x85, 0x04, 0xb8, 0x98, 0x13, 0xd3, 0x53, 0x25, 0x98, 0x14, 0x98, 0x34, 0x58, 0x83, 0x40,  0X4C, 0x21, 0x11, 0x2e, 0xd6, 0xd4, 0xdc, 0xc4, 0XCC, 0x1c, 0x09, 0x66, 0x05, 0x46, 0x0d, 0xce, 0x20, 0x08, 0x07, 0x10, 0x00, 0x00, 0xFF, 0xFF, 0x16, 0x57, 0xf0, 0x40, 0x5c, 0x00, 0x00, 0x00,}

As you can see, PROTOC based on our Test.proto file, generated test.pb.go, defines the relevant structure, the related fields in the relevant structure of the Read method, and some other methods.

3. Questions

When I execute the above command under the CMD terminal, I always say Missing input file the error, but I don't know why. And I am in git bash, if I do not enter the directory of the Test.proto file, it is also reported Missing input file wrong. These two problems have not been solved.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.