日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Go 支持Protocol Buffers的配置

發(fā)布時(shí)間:2025/7/25 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Go 支持Protocol Buffers的配置 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Go 支持Protocol Buffers的配置


安裝

protoc (The protocol compiler)是由C++寫的,支持的 C++、Java、Python、Objective-C、C#、JavaNano、JavaScript、Ruby、PHP 的實(shí)現(xiàn)都在 https://github.com/google/protobuf 這個(gè)項(xiàng)目中, 例外的是 Go 的實(shí)現(xiàn)是在 https://github.com/golang/protobuf 這里。

所以Go 支持 Protocol Buffers的安裝需要下面三步:

  • Install the standard C++ implementation of protocol buffers from https://developers.google.com/protocol-buffers/ 。如果不想源碼編譯的話,可以直接下載對(duì)應(yīng)操作系統(tǒng)對(duì)應(yīng)的 protoc https://github.com/google/protobuf/releases, 參考 : https://github.com/google/protobuf/releases?
  • Of course, install the Go compiler and tools from https://golang.org/ See https://golang.org/doc/install for details or, if you are using gccgo, follow the instructions at https://golang.org/doc/install/gccgo?? 安裝Go的開發(fā)編譯環(huán)境。
  • Grab the code from the repository and install the proto package. The simplest way is to run go get -u github.com/golang/protobuf/{proto,protoc-gen-go}. The compiler plugin, protoc-gen-go, will be installed in $GOBIN, defaulting to $GOPATH/bin. It must be in your $PATH for the protocol compiler, protoc, to find it.通過go get -u github.com/golang/protobuf/{proto,protoc-gen-go}? 獲得go的插件包,并完成 protoc-gen-go 的編譯。protoc-gen-go 默認(rèn)會(huì)編譯到 $GOPATH/bin 目錄下, 這個(gè)目錄需要放入$PATH 中,方便 protoc 找到 protoc-gen-go 。

?

https://github.com/golang/protobuf 這里其實(shí)包含兩部分功能:

  • a 'protocol compiler plugin' that generates Go source files that, once compiled, can access and manage protocol buffers;? Protoc編譯插件,產(chǎn)生Go的源碼。
  • a library that implements run-time support for encoding (marshaling), decoding (unmarshaling), and accessing protocol buffers. 運(yùn)行時(shí)支持庫(kù),包括編碼、解碼、訪問PB內(nèi)容的功能庫(kù)。

?

使用 protoc

把 proto 文件編譯成 go 源碼的命令如下:

$ protoc --go_out=./go/ ./proto/helloworld.proto

參考: http://www.cnblogs.com/ghj1976/p/5435565.html?

如果我們需要生產(chǎn) gRPC 使用的代碼, 則需要是下面的命令:

$ protoc --go_out=plugins=grpc:./go/ ./proto/helloworld.proto

注意這類表明了 plugins=grpc,

按照這種方式生成的代碼,會(huì)多 客戶端和服務(wù)器端的通用代碼,方便我們建立客戶端和服務(wù)器端的服務(wù)。

下面是使用helloworld.proto 產(chǎn)生的 grpc 使用的源碼, 比不帶 grpc的要多一部分代碼。

// Code generated by protoc-gen-go.
// source: proto/helloworld.proto
// DO NOT EDIT!

/*
Package helloworld is a generated protocol buffer package.

It is generated from these files:
??? proto/helloworld.proto

It has these top-level messages:
??? HelloRequest
??? HelloReply
*/
package helloworld

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"

import (
??? context "golang.org/x/net/context"
??? grpc "google.golang.org/grpc"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
const _ = proto.ProtoPackageIsVersion1

// The request message containing the user's name.
type HelloRequest struct {
??? Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
}

func (m *HelloRequest) Reset()??????????????????? { *m = HelloRequest{} }
func (m *HelloRequest) String() string??????????? { return proto.CompactTextString(m) }
func (*HelloRequest) ProtoMessage()?????????????? {}
func (*HelloRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }

// The response message containing the greetings
type HelloReply struct {
??? Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"`
}

func (m *HelloReply) Reset()??????????????????? { *m = HelloReply{} }
func (m *HelloReply) String() string??????????? { return proto.CompactTextString(m) }
func (*HelloReply) ProtoMessage()?????????????? {}
func (*HelloReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }

func init() {
??? proto.RegisterType((*HelloRequest)(nil), "helloworld.HelloRequest")
??? proto.RegisterType((*HelloReply)(nil), "helloworld.HelloReply")
}

// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn

// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion2

// Client API for Greeter service

type GreeterClient interface {
??? // Sends a greeting
??? SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)
}

type greeterClient struct {
??? cc *grpc.ClientConn
}

func NewGreeterClient(cc *grpc.ClientConn) GreeterClient {
??? return &greeterClient{cc}
}

func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) {
??? out := new(HelloReply)
??? err := grpc.Invoke(ctx, "/helloworld.Greeter/SayHello", in, out, c.cc, opts...)
??? if err != nil {
??????? return nil, err
??? }
??? return out, nil
}

// Server API for Greeter service

type GreeterServer interface {
??? // Sends a greeting
??? SayHello(context.Context, *HelloRequest) (*HelloReply, error)
}

func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) {
??? s.RegisterService(&_Greeter_serviceDesc, srv)
}

func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
??? in := new(HelloRequest)
??? if err := dec(in); err != nil {
??????? return nil, err
??? }
??? if interceptor == nil {
??????? return srv.(GreeterServer).SayHello(ctx, in)
??? }
??? info := &grpc.UnaryServerInfo{
??????? Server:???? srv,
??????? FullMethod: "/helloworld.Greeter/SayHello",
??? }
??? handler := func(ctx context.Context, req interface{}) (interface{}, error) {
??????? return srv.(GreeterServer).SayHello(ctx, req.(*HelloRequest))
??? }
??? return interceptor(ctx, in, info, handler)
}

var _Greeter_serviceDesc = grpc.ServiceDesc{
??? ServiceName: "helloworld.Greeter",
??? HandlerType: (*GreeterServer)(nil),
??? Methods: []grpc.MethodDesc{
??????? {
??????????? MethodName: "SayHello",
??????????? Handler:??? _Greeter_SayHello_Handler,
??????? },
??? },
??? Streams: []grpc.StreamDesc{},
}

var fileDescriptor0 = []byte{
??? // 183 bytes of a gzipped FileDescriptorProto
??? 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x12, 0x2b, 0x28, 0xca, 0x2f,
??? 0xc9, 0xd7, 0xcf, 0x48, 0xcd, 0xc9, 0xc9, 0x2f, 0xcf, 0x2f, 0xca, 0x49, 0xd1, 0x03, 0x0b, 0x08,
??? 0x71, 0x21, 0x44, 0x94, 0x94, 0xb8, 0x78, 0x3c, 0x40, 0xbc, 0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2,
??? 0x12, 0x21, 0x21, 0x2e, 0x96, 0xbc, 0xc4, 0xdc, 0x54, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xce, 0x20,
??? 0x30, 0x5b, 0x49, 0x8d, 0x8b, 0x0b, 0xaa, 0xa6, 0x20, 0xa7, 0x52, 0x48, 0x82, 0x8b, 0x3d, 0x37,
??? 0xb5, 0xb8, 0x38, 0x31, 0x1d, 0xa6, 0x08, 0xc6, 0x35, 0xf2, 0xe4, 0x62, 0x77, 0x2f, 0x4a, 0x4d,
??? 0x2d, 0x49, 0x2d, 0x12, 0xb2, 0xe3, 0xe2, 0x08, 0x4e, 0xac, 0x04, 0xeb, 0x12, 0x92, 0xd0, 0x43,
??? 0x72, 0x01, 0xb2, 0x65, 0x52, 0x62, 0x58, 0x64, 0x80, 0x56, 0x28, 0x31, 0x38, 0x99, 0x71, 0x49,
??? 0x67, 0xe6, 0xeb, 0xa5, 0x17, 0x15, 0x24, 0xeb, 0xa5, 0x56, 0x24, 0xe6, 0x16, 0xe4, 0xa4, 0x16,
??? 0x23, 0xa9, 0x75, 0xe2, 0x07, 0x2b, 0x0e, 0x07, 0xb1, 0x03, 0x40, 0x5e, 0x0a, 0x60, 0x5c, 0xc4,
??? 0xc4, 0xec, 0xe1, 0x13, 0x9e, 0xc4, 0x06, 0xf6, 0xa1, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x3e,
??? 0x3f, 0x8e, 0x69, 0xfb, 0x00, 0x00, 0x00,
}

protoc-gen-go 的一些特有的參數(shù)如下:

命令格式:

protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
  • import_prefix=xxx - a prefix that is added onto the beginning of all imports. Useful for things like generating protos in a subdirectory, or regenerating vendored protobufs in-place.??? import 的前綴,當(dāng)產(chǎn)生protos在一個(gè)子目錄時(shí)有用。
  • import_path=foo/bar - used as the package if no input files declare go_package. If it contains slashes, everything up to the rightmost slash is ignored.沒有定義 go_package 時(shí)的包名。?
  • plugins=plugin1+plugin2 - specifies the list of sub-plugins to load. The only plugin in this repo is grpc.? 插件名,這里只有一個(gè)插件 grpc
  • Mfoo/bar.proto=quux/shme - declares that foo/bar.proto is associated with Go package quux/shme. This is subject to the import_prefix parameter. 定義proto文件跟包的映射關(guān)系。

?

參考資料:

https://github.com/golang/protobuf


總結(jié)

以上是生活随笔為你收集整理的Go 支持Protocol Buffers的配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。