
- Protocol Buffers - Home
- Protocol Buffers - Introduction
- Protocol Buffers - Environment Setup
- Protocol Buffers - Basic App
- Protocol Buffers - Constructs
- Protocol Buffers - message
- Protocol Buffers - string
- Protocol Buffers - Numbers
- Protocol Buffers - bool
- Protocol Buffers - enum
- Protocol Buffers - repeated
- Protocol Buffers - map
- Protocol Buffers - Nested Class
- Protocol Buffers - Optionality & Defaults
- Protocol Buffers - Language Independence
- Protocol Buffers - Compound Data Types
- Protocol Buffers - Command Line Usage
- Protocol Buffers - Rules to Update Definition
- Protocol Buffers - Integration with Kafka
- Protocol Buffers - In Other Languages
- Protocol Buffers Useful Resources
- Protocol Buffers - Quick Guide
- Protocol Buffers - Useful Resources
- Protocol Buffers - Discussion
Protocol Buffers - In Other Languages
We have been using Protocol Buffers in Java and Python. But there are multiple languages it supports including C++, C#, Kotlin, 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 −
greeting.proto
syntax = "proto3"; package tutorial; message Greet { string greeting = 1; string username = 2; }
Using Google Protocol Buffers in Go Lang
To use the above Protocol Buffers 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 Protocol Buffers 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 serialization and deserialization of the Greet object.
Now, let us create the writer of the data, which will take the username and greeting as its input −
greeting_writer.go
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 −
greeting_reader.go
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) }
Output
The reader simply reads from the same file, deserializes 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 serialized by the writer and saved to the file, that exact data is correctly deserialized by the reader and printed accordingly.
Using Google Protocol Buffers in Dart
To use the above Protocol Buffers 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 Protocol Buffers 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 serialization and deserialization of the Greet object.
Now, let us create the writer of the data, which will take the username and greeting as its input −
greeting_writer.dart
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 −
greeting_reader.dart
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) }
Output
The reader simply reads from the same file, deserializes 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 serialized by the writer and saved to the file, that exact data is correctly deserialized by the reader and printed accordingly.