一、protobuf構建
protobuf是一種輕量級、高效、平台無關、語言無關、擴展性強的數據序列化和反序列化協議。在protobuf中,開發者可以通過定義消息格式,自動生成數據訪問API,從而達到數據傳輸和存儲的目的。
protobufgo是Golang社區提供的一種開源工具,旨在為Golang開發者提供高效、靈活、跨語言的protobuf序列化和反序列化功能。使用protobufgo,我們可以方便地通過protobuf定義文件,生成Golang訪問API,並且可以與其他語言的protobuf代碼進行互操作。
我們可以通過以下步驟來構建protobufgo:
// 安裝protobuf工具 $ go get github.com/golang/protobuf/proto // 安裝protobufgogo插件 $ go get github.com/gogo/protobuf/proto $ go get github.com/gogo/protobuf/protoc-gen-gogo
二、protobuf工具
protobufgo提供了許多有用的工具,可以幫助我們更輕鬆地開發和維護代碼。以下是一些常用的工具:
1. protoc
protoc是protobuf官方提供的編譯器,負責將protobuf定義文件編譯成不同語言的訪問API。protobufgo通過gogo/protobuf插件對protoc進行了擴展,提供了更多的選項和功能,例如生成更快的序列化和反序列化方法、支持不同語言之間的互操作等。
2. protoc-gen-go
protoc-gen-go是protobuf官方提供的Golang插件,用於將protobuf定義文件編譯成Golang訪問API。protobufgo提供了gogo/protobuf插件,可以生成更高效的序列化和反序列化方法,也可以支持更多的選項和功能,例如生成google.golang.org/genproto包下的Golang protobuf代碼。
3. protoc-gen-gofast
protoc-gen-gofast是protobufgo提供的一個更快的Golang插件,它基於gogo/protobuf插件進行了優化,生成的代碼速度更快。
4. protoc-gen-grpc-gateway
protoc-gen-grpc-gateway是一個開源的grpc轉換代理,可以將標準的grpc服務轉換成RESTful的API接口,支持多種編程語言,包括Golang。
三、protobuf公參
protobuf定義文件定義了消息的結構和字段信息,類似於Golang中的struct,包括以下4個要素:
1. package
指定生成的代碼所在的Golang代碼包。
2. import
導入其他protobuf定義文件。
3. message
定義消息結構和字段信息。
4. service
定義服務器和客戶端之間的遠程調用接口。
以下是一個簡單的protobuf定義文件的示例:
syntax = "proto3"; package example; import "google/protobuf/timestamp.proto"; message Person { string name = 1; int32 age = 2; repeated string email = 3; google.protobuf.Timestamp created_time = 4; } service PersonService { rpc GetPerson (PersonRequest) returns (PersonResponse); } message PersonRequest { string name = 1; } message PersonResponse { Person person = 1; }
四、protobuf構建與移植
protobufgo提供了多種選項和調用方式,以滿足不同平台和應用場景的需要。總體而言,protobufgo可以分為以下3個部分進行構建和移植:
1. protoc編譯器
protoc編譯器是protobufgo的核心,可以將protobuf定義文件編譯成不同語言的訪問API。protobufgo通過gogo/protobuf插件對protoc進行了擴展,提供了更多的選項和功能,例如生成更快的序列化和反序列化方法、支持不同語言之間的互操作等。以下是一些常用的命令:
// 編譯proto文件 $ protoc --proto_path=$GOPATH/src:. --gogo_out=. file.proto // 支持生成其他語言的代碼,例如Java: $ protoc --proto_path=$GOPATH/src:. --java_out=. file.proto
2. gogo/protobuf插件
gogo/protobuf插件是protobufgo提供的一種高效、靈活、跨語言的protobuf序列化和反序列化庫。在Golang應用程序中使用protobufgo,需要先將protobuf定義文件編譯成Golang訪問API,然後將生成的代碼導入到應用程序中。以下是一些常用的命令:
// 安裝插件 $ go get github.com/gogo/protobuf/proto $ go get github.com/gogo/protobuf/protoc-gen-gogo // 編譯proto文件 $ protoc --proto_path=$GOPATH/src:. --gogo_out=. file.proto
3. 應用程序
在應用程序中使用protobufgo,需要導入protobuf生成的Golang代碼,並定義和使用消息結構體。
import "example/file.pb.go" func main() { person := &example.Person{ Name: "Tom", Age: 28, Email: []string{"tom@example.com", "tom@gmail.com"}, CreatedTime: ×tamp.Timestamp{ Seconds: time.Now().Unix(), }, } data, _ := proto.Marshal(person) newPerson := &example.Person{} _ = proto.Unmarshal(data, newPerson) }
五、protobuf共享內存
protobufgo支持將消息數組序列化到共享內存中,以實現進程之間的數據傳遞和共享。以下是一些常用的命令:
// 獲取消息佔用的共享內存大小 size := proto.Size(msg) // 分配共享內存 shmID, err := ipc.ShmGet(1, size, ipc.IPC_CREAT|ipc.IPC_EXCL|0666) // 將消息序列化到共享內存 msgBytes, err := proto.Marshal(msg) shmbuf, err := ipc.Shmat(shmID, uintptr(0), 0) copy((*((*[1 << 30]byte)(unsafe.Pointer(shmbuf))))[:size], msgBytes) ipc.Shmdt(shmbuf) // 從共享內存中反序列化消息 shmbuf, err = ipc.Shmat(shmID, uintptr(0), 0) unmarshMsg := &T{} _ = proto.Unmarshal(shmbuf[:size], unmarshMsg) ipc.Shmdt(shmbuf)
六、protobuf工具轉json
protobufgo支持將消息序列化為JSON字符串,以方便在HTTP API中傳輸和返回。以下是一些常用的命令:
// 將消息序列化為JSON字符串 buf, err := protojson.Marshal(msg) // 將JSON字符串反序列化為消息 newMsg := &T{} _ = protojson.Unmarshal(buf, newMsg)
上述代碼片段中的T代表消息結構體。
七、總結
protobufgo是一種高效、靈活、跨語言的數據序列化和反序列化協議,提供了強大的命令工具和訪問API,可以幫助我們更輕鬆地開發和維護代碼。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/196099.html