grpc

package
v0.0.0-...-d944f76 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2021 License: MIT Imports: 10 Imported by: 0

README

grpc

Package grpc is a very simple introduction to creating a GRPC API definition using a .proto file and generating code for it using the protoc compiler.

Setting up protoc and Go code generators

The protoc compiler can be downloaded from https://github.com/protocolbuffers/protobuf/releases/.

We need to install two Go plugins, which generate Go structs for protocol buffer messages and scaffolding for a GRPC server and client respectively. This can be done by running the following command:

go get google.golang.org/protobuf/cmd/protoc-gen-go \
       google.golang.org/grpc/cmd/protoc-gen-go-grpc

This command will install the relevant executables to either $GOBIN or ${GOPATH}/bin. The directory should be on your PATH for the protoc compiler to find them.

Generating core from the .proto file

The following command will generate both the structs and the scaffolding.

protoc --go_out=. --go_opt=paths=source_relative \
       --go-grpc_out=. --go-grpc_opt=paths=source_relative \
       contacts.proto

This will generate Go source files in the same directory. The file contacts.pb.go will contain structs derived from the messages defined in the .proto file. The file contacts_grpc.pb.go will contain a fully implemented GRPC client for the service defined in the .proto file. It will also contain scaffolding for implementing a GRPC server for the service, notably an interface that has to be implemented when writing the actual GRPC server.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ContactService_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "contacts.ContactService",
	HandlerType: (*ContactServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "NewContact",
			Handler:    _ContactService_NewContact_Handler,
		},
		{
			MethodName: "GetContactById",
			Handler:    _ContactService_GetContactById_Handler,
		},
	},
	Streams: []grpc.StreamDesc{
		{
			StreamName:    "GetAllContacts",
			Handler:       _ContactService_GetAllContacts_Handler,
			ServerStreams: true,
		},
	},
	Metadata: "contacts.proto",
}

ContactService_ServiceDesc is the grpc.ServiceDesc for ContactService service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

View Source
var File_contacts_proto protoreflect.FileDescriptor

Functions

func RegisterContactServiceServer

func RegisterContactServiceServer(s grpc.ServiceRegistrar, srv ContactServiceServer)

Types

type Contact

type Contact struct {
	Id    int32  `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"`
	Name  string `protobuf:"bytes,2,opt,name=Name,proto3" json:"Name,omitempty"`
	Phone string `protobuf:"bytes,3,opt,name=Phone,proto3" json:"Phone,omitempty"`
	Email string `protobuf:"bytes,4,opt,name=Email,proto3" json:"Email,omitempty"`
	City  string `protobuf:"bytes,5,opt,name=City,proto3" json:"City,omitempty"`
	Age   int32  `protobuf:"varint,6,opt,name=Age,proto3" json:"Age,omitempty"`
	// contains filtered or unexported fields
}

func (*Contact) Descriptor deprecated

func (*Contact) Descriptor() ([]byte, []int)

Deprecated: Use Contact.ProtoReflect.Descriptor instead.

func (*Contact) GetAge

func (x *Contact) GetAge() int32

func (*Contact) GetCity

func (x *Contact) GetCity() string

func (*Contact) GetEmail

func (x *Contact) GetEmail() string

func (*Contact) GetId

func (x *Contact) GetId() int32

func (*Contact) GetName

func (x *Contact) GetName() string

func (*Contact) GetPhone

func (x *Contact) GetPhone() string

func (*Contact) ProtoMessage

func (*Contact) ProtoMessage()

func (*Contact) ProtoReflect

func (x *Contact) ProtoReflect() protoreflect.Message

func (*Contact) Reset

func (x *Contact) Reset()

func (*Contact) String

func (x *Contact) String() string

type ContactServiceClient

type ContactServiceClient interface {
	NewContact(ctx context.Context, in *Contact, opts ...grpc.CallOption) (*Contact, error)
	GetAllContacts(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (ContactService_GetAllContactsClient, error)
	GetContactById(ctx context.Context, in *GetContactInput, opts ...grpc.CallOption) (*Contact, error)
}

ContactServiceClient is the client API for ContactService service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.

type ContactServiceServer

type ContactServiceServer interface {
	NewContact(context.Context, *Contact) (*Contact, error)
	GetAllContacts(*emptypb.Empty, ContactService_GetAllContactsServer) error
	GetContactById(context.Context, *GetContactInput) (*Contact, error)
	// contains filtered or unexported methods
}

ContactServiceServer is the server API for ContactService service. All implementations must embed UnimplementedContactServiceServer for forward compatibility

type ContactService_GetAllContactsClient

type ContactService_GetAllContactsClient interface {
	Recv() (*Contact, error)
	grpc.ClientStream
}

type ContactService_GetAllContactsServer

type ContactService_GetAllContactsServer interface {
	Send(*Contact) error
	grpc.ServerStream
}

type GetContactInput

type GetContactInput struct {
	Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"`
	// contains filtered or unexported fields
}

func (*GetContactInput) Descriptor deprecated

func (*GetContactInput) Descriptor() ([]byte, []int)

Deprecated: Use GetContactInput.ProtoReflect.Descriptor instead.

func (*GetContactInput) GetId

func (x *GetContactInput) GetId() int32

func (*GetContactInput) ProtoMessage

func (*GetContactInput) ProtoMessage()

func (*GetContactInput) ProtoReflect

func (x *GetContactInput) ProtoReflect() protoreflect.Message

func (*GetContactInput) Reset

func (x *GetContactInput) Reset()

func (*GetContactInput) String

func (x *GetContactInput) String() string

type UnimplementedContactServiceServer

type UnimplementedContactServiceServer struct {
}

UnimplementedContactServiceServer must be embedded to have forward compatible implementations.

func (UnimplementedContactServiceServer) GetAllContacts

func (UnimplementedContactServiceServer) GetContactById

func (UnimplementedContactServiceServer) NewContact

type UnsafeContactServiceServer

type UnsafeContactServiceServer interface {
	// contains filtered or unexported methods
}

UnsafeContactServiceServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to ContactServiceServer will result in compilation errors.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL