您的位置:首页 > 编程语言 > Go语言

google/protobuf hello world

2016-08-06 07:56 375 查看
/(ㄒoㄒ)/~~官网被墙

1.github>Search>protobuforprotocolbuffers

2.https://github.com/google/protobuf

3.releases

4.protoc-3.0.0-alpha-2-win32.zip

readme.txt

ProtocolBuffers-Google'sdatainterchangeformat
Copyright2008GoogleInc.https://developers.google.com/protocol-buffers/
ThispackagecontainsaprecompiledWin32binaryversionoftheprotocolbuffer
compiler(protoc).ThisbinaryisintendedforWindowsuserswhowantto
useProtocolBuffersinJavaorPythonbutdonotwanttocompileprotoc
themselves.Toinstall,simplyplacethisbinarysomewhereinyourPATH.

ThisbinarywasbuiltusingMinGW,buttheoutputisthesameregardlessof
theC++compilerused.

Youwillstillneedtodownloadthesourcecodepackageinordertoobtainthe
JavaorPythonruntimelibraries.Getitfrom:'target='_blank'>https://github.com/google/protobuf/releases/[/code]
将protoc.exe放至C:\WINDOWS\system32

cmd

protoc--version

libprotoc3.0.0


protoc-h

Usage:protoc[OPTION]PROTO_FILES
ParsePROTO_FILESandgenerateoutputbasedontheoptionsgiven:
-IPATH,--proto_path=PATHSpecifythedirectoryinwhichtosearchfor
imports.Maybespecifiedmultipletimes;
directorieswillbesearchedinorder.Ifnot
given,thecurrentworkingdirectoryisused.
--versionShowversioninfoandexit.
-h,--helpShowthistextandexit.
--encode=MESSAGE_TYPEReadatext-formatmessageofthegiventype
fromstandardinputandwriteitinbinary
tostandardoutput.Themessagetypemust
bedefinedinPROTO_FILESortheirimports.
--decode=MESSAGE_TYPEReadabinarymessageofthegiventypefrom
standardinputandwriteitintextformat
tostandardoutput.Themessagetypemust
bedefinedinPROTO_FILESortheirimports.
--decode_rawReadanarbitraryprotocolmessagefrom
standardinputandwritetherawtag/value
pairsintextformattostandardoutput.No
PROTO_FILESshouldbegivenwhenusingthis
flag.
-oFILE,WritesaFileDescriptorSet(aprotocolbuffer,
--descriptor_set_out=FILEdefinedindescriptor.proto)containingallof
theinputfilestoFILE.
--include_importsWhenusing--descriptor_set_out,alsoinclude
alldependenciesoftheinputfilesinthe
set,sothatthesetisself-contained.
--include_source_infoWhenusing--descriptor_set_out,donotstrip
SourceCodeInfofromtheFileDescriptorProto.
Thisresultsinvastlylargerdescriptorsthat
includeinformationabouttheoriginal
locationofeachdeclinthesourcefileas
wellassurroundingcomments.
--error_format=FORMATSettheformatinwhichtoprinterrors.
FORMATmaybe'gcc'(thedefault)or'msvs'
(MicrosoftVisualStudioformat).
--print_free_field_numbersPrintthefreefieldnumbersofthemessages
definedinthegivenprotofiles.Groupsshare
thesamefieldnumberspacewiththeparent
message.Extensionrangesarecountedas
occupiedfieldsnumbers.
--plugin=EXECUTABLESpecifiesapluginexecutabletouse.
Normally,protocsearchesthePATHfor
plugins,butyoumayspecifyadditional
executablesnotinthepathusingthisflag.
Additionally,EXECUTABLEmaybeoftheform
NAME=PATH,inwhichcasethegivenpluginname
ismappedtothegivenexecutableevenif
theexecutable'sownnamediffers.
--cpp_out=OUT_DIRGenerateC++headerandsource.
--java_out=OUT_DIRGenerateJavasourcefile.
--javanano_out=OUT_DIRGenerateJavaNanosourcefile.
--python_out=OUT_DIRGeneratePythonsourcefile.
--ruby_out=OUT_DIRGenerateRubysourcefile.


protoc--java_out=.addressbook.proto

[libprotobufWARNINGgoogle/protobuf/compiler/parser.cc:471]Nosyntaxspecifiedfortheprotofile.Pleaseuse'syntax
="proto2";'or'syntax="proto3";'tospecifyasyntaxversion.(Defaultedtoproto2syntax.)


遭遇以上问题时,请确保protofile中已指定syntax,然后重试。

具体查看https://github.com/google/protobuf/blob/master/CHANGES.txt

Anewnotion"syntax"isintroducedtospecifywhethera.protofile
usesproto2orproto3:

//foo.proto
syntax="proto3";
messageBar{...}


5.消息模板文件位置

clonehttps://github.com/google/protobuf.git
文件位于\protobuf\examples

6.maven依赖

<repositories>
<repository>
<id>oschina</id>
<url>http://maven.oschina.net/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.0.0-alpha-2</version>
</dependency>
</dependencies>


7.完整示例

helptree

tree/F

E:.
└─hello-protocol
│ADDRESS_BOOK_FILE
│pom.xml
│
└─src
└─main
└─java
└─cn
└─zxg
AddPerson.java
AddressBookProtos.java
ListPeople.java


执行AddPerson参数为ADDRESS_BOOK_FILE结果:

ADDRESS_BOOK_FILE:Filenotfound.Creatinganewfile.
EnterpersonID:7
Entername:James
Enteremailaddress(blankfornone):7@7.com
Enteraphonenumber(orleaveblanktofinish):911
Isthisamobile,home,orworkphone?work
Enteraphonenumber(orleaveblanktofinish):


执行ListPerson参数为ADDRESS_BOOK_FILE结果:

PersonID:7
Name:James
E-mailaddress:7@7.com
Workphone#:911



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: