- Protocol Buffers 教程
- Protocol Buffers - 首頁
- Protocol Buffers - 簡介
- Protocol Buffers - 基本應用
- Protocol Buffers - 結構
- Protocol Buffers - 訊息 (message)
- Protocol Buffers - 字串 (string)
- Protocol Buffers - 數字
- Protocol Buffers - 布林值 (bool)
- Protocol Buffers - 列舉 (enum)
- Protocol Buffers - 重複欄位 (repeated)
- Protocol Buffers - 對映 (map)
- Protocol Buffers - 巢狀類
- Protocol Buffers - 可選欄位和預設值
- Protocol Buffers - 語言無關性
- Protocol Buffers - 複合資料型別
- Protocol Buffers - 命令列用法
- Protocol Buffers - 更新定義規則
- Protocol Buffers - 與 Kafka 整合
- Protocol Buffers - 其他語言
- Protocol Buffers 有用資源
- Protocol Buffers - 快速指南
- Protocol Buffers - 有用資源
- Protocol Buffers - 討論
Protocol Buffers - 其他語言
我們一直在 Java 和 Python 中使用 Protocol Buffers。但是它支援多種語言,包括 C++、C#、Kotlin、Dart、Go 等。基本內容大致相同,即編寫一個 proto 模式,透過我們程式碼可以使用的 protoc 二進位制檔案生成原始碼。讓我們在本節中為 Go 和 Dart 編寫一個基本示例。
我們將使用以下 proto 檔案:
greeting.proto
syntax = "proto3";
package tutorial;
message Greet {
string greeting = 1;
string username = 2;
}
在 Go 語言中使用 Google Protocol Buffers
要使用上述 Protocol Buffers 檔案,我們首先需要為 Go 語言中的 Greet 類生成程式碼。為此,我們需要執行以下操作:
安裝 Go Protocol Buffers 外掛 (protoc-gen-go),這是我們一直在使用的 protoc 檔案的先決條件:
go install google.golang.org/protobuf/cmd/protoc-gen-go
然後,使用提供的 ".proto" 檔案執行 protoc,我們將指示它在 "go" 目錄下生成程式碼。
protoc --go_out=go proto_files/greeting.proto
執行上述命令後,您會注意到一個自動生成的類:"greeting.pb.go"。此類將幫助我們對 Greet 物件進行序列化和反序列化。
現在,讓我們建立資料的 寫入器,它將接收 使用者名稱 和 問候語 作為輸入:
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)
}
現在讓我們建立讀取檔案的 讀取器:
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)
}
輸出
讀取器 簡單地從同一檔案讀取資料,對其進行反序列化,然後列印問候資訊。
現在我們已經設定了 寫入器 和 讀取器,讓我們編譯專案。
接下來,讓我們首先執行 寫入器:
go run greeting_writer.go
Saved greeting with following data to disk:
{greeting: Hello, username: John}
然後,讓我們執行 讀取器:
go run greeting_reader.go
Reading from file greeting_protobuf_output
{greeting: Hello, username: John}
因此,正如我們所看到的,寫入器 序列化並儲存到檔案的資料,被 讀取器 正確地反序列化並相應地打印出來。
在 Dart 中使用 Google Protocol Buffers
要使用上述 Protocol Buffers 檔案,我們首先需要安裝併為 Dart 語言中的 Greet 類生成程式碼。為此,我們需要執行以下操作:
安裝 Dart Protocol Buffers 外掛 (protoc-gen-dart),這是我們一直在使用的 protoc 檔案的先決條件。 https://github.com/dart-lang/protobuf/tree/master/protoc_plugin#how-to-build-and-use
然後,使用提供的 ".proto" 檔案執行 protoc,我們將指示它在 "dart" 目錄下生成程式碼。
protoc --go_out=dart proto_files/greeting.proto
執行上述命令後,您會注意到一個自動生成的類:"greeting.pb.dart"。此類將幫助我們對 Greet 物件進行序列化和反序列化。
現在,讓我們建立資料的 寫入器,它將接收 使用者名稱 和 問候語 作為輸入:
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)
}
接下來,讓我們建立一個讀取檔案的 讀取器:
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)
}
輸出
讀取器 簡單地從同一檔案讀取資料,對其進行反序列化,然後列印 問候資訊。
現在我們已經設定了 寫入器 和 讀取器,讓我們編譯專案。
接下來,讓我們首先執行 寫入器:
dart run greeting_writer.dart Saved greeting with following data to disk: greeting: Hello username: John
然後,讓我們執行 讀取器。
dart run greeting_reader.dart Reading from file greeting_protobuf_output greeting: Hello username: John
因此,正如我們所看到的,寫入器 序列化並儲存到檔案的資料,被讀取器正確地反序列化並相應地打印出來。