Quick ProtoBuf
1 Install protobuf compiler
protoc
takes*.proto
files as inputs
sudo dnf install protobuf-compiler
protoc by default only outputs cpp, java, js ,python ,c# ,php,ruby,objc.
Notice it DOES NOT output golang (we need to install a plugin)
2 setup for golang
go env GOPATH
#/home/rhel/go
The above command works if you have go installed but sometimes your terminal doesnt set $GOPATH
echo $GOPATH
if it doesnt exist then modify your .zshrc
the .zshrc
file should look like below
{bash filename=.zshrc} export GOPATH=$(go env GOPATH) export GOBIN=$GOPATH/bin export PATH=$PATH:$GOPATH:$GOBIN
install protoc plugin for golang
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
3 Example MTA API
go mod init mydemo
- Download the
gtfs-realtime.proto
file - Download
go get google.golang.org/protobuf
go mod download github.com/golang/protobuf
make a folder named out
protoc -I=./ --go_out=./ ./gtfs-realtime.proto
protoc -I=./ --go_out=./ ./nyct-subway.proto
In the gtfs-realtime.proto
add the line
option go_package = "/bleh";
Then it will work and create a file gtfs-realtime.pb.go
**notice that we import pb "mydemo/bleh"
because mydemo
is our regular golang package name, and bleh
is the folder holding our .pb.go
file generated from option go_package = "/bleh";
package main
import(
// pb "[root_golang_package_name]/[proto_file_option_go_name]"
"mydemo/bleh"
pb "fmt"
)
func main(){
.Println(pb.FeedHeader_FULL_DATASET)
fmt}
curl -H 'x-api-key: XXXXXXXXXXXXXXXXXXXXXXXXXX' \
-O \
-L https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-bdfm
4 protoc compiler
= "proto3";
syntax
= "google.golang.org/grpc/examples/helloworld/helloworld";
option go_package = true;
option java_multiple_files = "io.grpc.examples.helloworld";
option java_package = "HelloWorldProto";
option java_outer_classname
package helloworld;
// The greeting service definition.
{
service Greeter // Sends a greeting
(HelloRequest) returns (HelloReply) {}
rpc SayHello }
// The request message containing the user's name.
{
message HelloRequest string name = 1;
}
// The response message containing the greetings
{
message HelloReply string message = 1;
}
- In golang protoc will take the
.proto
file to generate a.pb.go
- Golang uses struct composition aka struct embedding which is similar to class inheritance/extension in C++. Note Golang does this because it has no concept of class aka struct inheritance/extension.
//in Go we embed the protobuf Server into our server struct
type server struct {
.UnimplementedGreeterServer
pb}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
.Printf("Received: %v", in.GetName())
logreturn &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
func main() {
.Parse()
flag, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
lisif err != nil {
.Fatalf("failed to listen: %v", err)
log}
:= grpc.NewServer()
s .RegisterGreeterServer(s, &server{}) //IMPORTANT &server{}
pb.Printf("server listening at %v", lis.Addr())
logif err := s.Serve(lis); err != nil {
.Fatalf("failed to serve: %v", err)
log}
}
Observe the similarity between Golang and C++ server grpc implementation.
class GreeterServiceImpl final : public Greeter::Service {
(ServerContext* context, const HelloRequest* request, HelloReply* reply) override {
Status SayHellostd::string prefix("Hello ");
->set_message(prefix + request->name());
replyreturn Status::OK;
}
};
void main(){
;
GreeterServiceImpl service...
.RegisterService(&service);
builder// Finally assemble the server.
}