English 中文(简体)
Protobuf - In Other Languages
  • 时间:2024-11-03

Protobuf - In Other Languages


Previous Page Next Page  

We have been using Protobuf in Java and Python. But there are multiple languages it supports including C++, C#, Kotpn, Dart, Go, etc. The basic stuff mostly remains the same, i.e., writing a proto schema, generating the source code via protoc binary which our code can use. Let us write a basic example for Go and Dart as part of this section.

We will use the following proto file −


syntax = "proto3";
package tutorial;
message Greet {
   string greeting = 1;
   string username = 2;
}

Using Google Protobuf in Go Lang

To use the above Protobuf file, we will first have to generate the code for the Greet class in Go language. For that, we need to do the following −

Install the Go Protobuf plugin (protoc-gen-go) which is a prerequisite for the protoc file which we have been using −


go install google.golang.org/protobuf/cmd/protoc-gen-go

Then, run the protoc with the provided ".proto" file and we will instruct it to generate the code under the "go" directory.


protoc  --go_out=go proto_files/greeting.proto

Post execution of the above command, you will notice an auto-generated class − "greeting.pb.go". This class would help us with the seriapzation and deseriapzation of the Greet object.

Now, let us create the writer of the data, which will take the username and greeting as its input −


import "fmt"
import "io/ioutil"

func main() {
   greet := Greeting{}
   greet.username = "John"
   greet.greeting = "Hello"
   out, err := proto.Marshal(greet)
    
   ioutil.WriteFile("greeting_go_out", out , 0644)
    
   fmt.Println("Saved greeting with following data to disk:")
   fmt.Println(p)
}

Now let us create the reader which will read the file −


import "fmt"
import "io/ioutil"

func main() {
   in, err := ioutil.ReadFile("greeting_go_out")

   greet := &pb.Greet{}
   proto.Unmarshal(in, greet)

   fmt.Println("Reading from file greeting_protobuf_output:")
   fmt.Println(greet)
}

The reader simply reads from the same file, deseriapzes it, and prints the data about the greeting.

Now that we have setup the reader and the writer, let us compile the project.

Next, let us first execute the writer


go run greeting_writer.go

Saved greeting with following data to disk:
{greeting: Hello, username: John}

Then, let us execute the reader


go run greeting_reader.go

Reading from file greeting_protobuf_output
{greeting: Hello, username: John}

So, as we can see, the data that was seriapzed by the writer and saved to the file, that exact data is correctly deseriapzed by the reader and printed accordingly.

Using Google Protobuf in Dart

To use the above Protobuf file, we will first have to install and generate the code for the Greet class in Dart language. For that, we need to do the following −

Install the Dart Protobuf plugin (protoc-gen-go) which is the prerequisite for the protoc file which we have been using. https://github.com/dart-lang/protobuf/tree/master/protoc_plugin#how-to-build-and-use

Then, run the protoc with the provided ".proto" file and we will instruct it to generate the code under the "dart" directory.


protoc  --go_out=dart proto_files/greeting.proto

Post execution of the above command, you will notice an auto-generated class − "greeting.pb.dart". This class would help us with the seriapzation and deseriapzation of the Greet object.

Now, let us create the writer of the data, which will take the username and greeting as its input −


import  dart:io ;
import  dart/greeting.pb.dart ;

main(List arguments) {
   Greeting greet = Greeting();
   greet.greeting = "Hello";
   greet.username = "John";
  
   File file = File("greeting_go_out");
  
   print("Saved greeting with following data to disk:")
   file.writeAsBytes(greet.writeToBuffer());
   print(greet)
}

Next, let us create a reader which will read the file −


import  dart:io ;
import  dart/greeting.pb.dart ;

main(List arguments) {
   File file = File("greeting_go_out");

   print("Reading from file greeting_protobuf_output:")
   Greeting greet = Greeting.fromBuffer(file.readAsBytesSync());
   print(greet)
}

The reader simply reads from the same file, deseriapzes it, and prints the data about the greeting.

Now that we have setup the reader and the writer, let us compile the project.

Next, let us first execute the writer


dart run greeting_writer.dart

Saved greeting with following data to disk:
greeting: Hello
username: John

And then, let us execute the reader.


dart run greeting_reader.dart

Reading from file greeting_protobuf_output
greeting: Hello
username: John

So, as we can see, the data that was seriapzed by the writer and saved to the file, that exact data is correctly deseriapzed by the reader and printed accordingly.

Advertisements