Go 中複雜結構的序列化和反序列化
在Go 中,高效地序列化和反序列化複雜資料結構(例如結構體)對於持久化至關重要儲存或網路通訊。
一個有效的方法是結合使用 Go 的 gob 套件使用base64編碼。 gob 套件為 Go 物件提供了一致的二進位序列化機制,而 base64 允許以字串格式方便而緊湊的表示。
以下是示範這種方法的範例:
package main import ( "encoding/base64" "encoding/gob" "bytes" "fmt" ) type Session struct { Properties map[string]interface{} Permissions []int64 } // GobBinaryMarshaller implements the Go BinaryMarshaller interface for Session struct. func (s *Session) GobBinaryMarshaller(b gob.GobEncoder, fieldName string) error { // Customize the binary encoding for Session struct. } // GobBinaryUnMarshaller implements the Go BinaryUnmarshaller interface for Session struct. func (s *Session) GobBinaryUnmarshaller(b gob.GobDecoder, fieldName string) error { // Customize the binary decoding for Session struct. } // ToGOB64 serializes the given Session struct into a base64-encoded string. func ToGOB64(m Session) string { b := bytes.Buffer{} e := gob.NewEncoder(&b) err := e.Encode(m) if err != nil { fmt.Println("failed gob Encode", err) } return base64.StdEncoding.EncodeToString(b.Bytes()) } // FromGOB64 deserializes the given base64-encoded string into a Session struct. func FromGOB64(str string) (*Session, error) { m := Session{} by, err := base64.StdEncoding.DecodeString(str) if err != nil { return nil, fmt.Errorf("failed base64 Decode: %v", err) } b := bytes.Buffer{} b.Write(by) d := gob.NewDecoder(&b) err = d.Decode(&m) if err != nil { return nil, fmt.Errorf("failed gob Decode: %v", err) } return &m, nil } func main() { // Register the Session type to enable gob encoding/decoding. gob.Register(Session{}) // Create a Session object. s := Session{Properties: make(map[string]interface{}), Permissions: []int64{1, 2, 3}} // Serialize the Session object into a base64-encoded string. encoded := ToGOB64(s) // Deserialize the Session object from the base64-encoded string. decoded, err := FromGOB64(encoded) if err != nil { fmt.Println("failed FromGOB64", err) return } // Verify that the decoded object is the same as the original. fmt.Println(s, decoded) }
透過註冊Session結構體帶有gob,我們自訂它的序列化和反序列化行為。這種方法為處理 Go 中更複雜的資料結構提供了靈活且高效能的解決方案。
以上是如何使用 gob 和 base64 在 Go 中高效地序列化和反序列化複雜結構?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

本文演示了創建模擬和存根進行單元測試。 它強調使用接口,提供模擬實現的示例,並討論最佳實踐,例如保持模擬集中並使用斷言庫。 文章

本文探討了GO的仿製藥自定義類型約束。 它詳細介紹了界面如何定義通用功能的最低類型要求,從而改善了類型的安全性和代碼可重複使用性。 本文還討論了局限性和最佳實踐

本文討論了GO的反思軟件包,用於運行時操作代碼,對序列化,通用編程等有益。它警告性能成本,例如較慢的執行和更高的內存使用,建議明智的使用和最佳

本文討論了GO中使用表驅動的測試,該方法使用測試用例表來測試具有多個輸入和結果的功能。它突出了諸如提高的可讀性,降低重複,可伸縮性,一致性和A

本文使用跟踪工具探討了GO應用程序執行流。 它討論了手冊和自動儀器技術,比較諸如Jaeger,Zipkin和Opentelemetry之類的工具,並突出顯示有效的數據可視化


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版