Quick ProtoBuf
1 Install protobuf compiler
protoctakes*.protofiles as inputs
sudo dnf install protobuf-compilerprotoc 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/goThe above command works if you have go installed but sometimes your terminal doesnt set $GOPATH
echo $GOPATHif 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@latest3 Example MTA API
go mod init mydemo- Download the
gtfs-realtime.protofile - Download
go get google.golang.org/protobuf
go mod download github.com/golang/protobufmake a folder named out
protoc -I=./ --go_out=./ ./gtfs-realtime.proto
protoc -I=./ --go_out=./ ./nyct-subway.protoIn 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]"
pb "mydemo/bleh"
"fmt"
)
func main(){
fmt.Println(pb.FeedHeader_FULL_DATASET)
}curl -H 'x-api-key: XXXXXXXXXXXXXXXXXXXXXXXXXX' \
-O \
-L https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-bdfm4 protoc compiler
syntax = "proto3";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// 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
.protofile 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 {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
func main() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{}) //IMPORTANT &server{}
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}Observe the similarity between Golang and C++ server grpc implementation.
class GreeterServiceImpl final : public Greeter::Service {
Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override {
std::string prefix("Hello ");
reply->set_message(prefix + request->name());
return Status::OK;
}
};
void main(){
GreeterServiceImpl service;
...
builder.RegisterService(&service);
// Finally assemble the server.
}