使用Go工具进行Protobuf编译

huangapple go评论75阅读模式
英文:

Protobuf compilation with Go tools

问题

有没有一种好的方法将protobuf编译与go build命令集成在一起?

英文:

Is there a good way to integrate protobuf compiles with the go build command?

答案1

得分: 2

goprotobuf "提供了Go的支持,以库和协议编译器插件的形式,用于Google的协议缓冲区"。

goprotobuf库的README中有一些很好的信息。

从https://code.google.com/p/goprotobuf/source/browse/README#106:

>考虑包含以下内容的test.proto文件

	package example;
	
	enum FOO { X = 17; };
	
	message Test {
	  required string label = 1;
	  optional int32 type = 2 [default=77];
	  repeated int64 reps = 3;
	  optional group OptionalGroup = 4 {
	    required string RequiredField = 5;
	  }
	}

>要从test.proto文件和其他一些Go文件构建一个包,可以编写如下的Makefile:

	include $(GOROOT)/src/Make.$(GOARCH)

	TARG=path/to/example
	GOFILES=\
		test.pb.go\
		other.go

	include $(GOROOT)/src/Make.pkg
	include $(GOROOT)/src/pkg/code.google.com/p/goprotobuf/Make.protobuf
英文:

goprotobuf "provides Go support, in the form of a library and protocol compiler plugin, for Google's protocol buffers".

The README at the goprotobuf library has some good info.

From https://code.google.com/p/goprotobuf/source/browse/README#106 :

>Consider file test.proto, containing

	package example;
	
	enum FOO { X = 17; };
	
	message Test {
	  required string label = 1;
	  optional int32 type = 2 [default=77];
	  repeated int64 reps = 3;
	  optional group OptionalGroup = 4 {
	    required string RequiredField = 5;
	  }
	}

>To build a package from test.proto and some other Go files, write a Makefile like this:

	include $(GOROOT)/src/Make.$(GOARCH)

	TARG=path/to/example
	GOFILES=\
		test.pb.go\
		other.go

	include $(GOROOT)/src/Make.pkg
	include $(GOROOT)/src/pkg/code.google.com/p/goprotobuf/Make.protobuf

huangapple
  • 本文由 发表于 2013年6月25日 08:01:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/17287087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定