溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Golang怎么使用gob實現(xiàn)結(jié)構(gòu)體的序列化

發(fā)布時間:2023-03-08 09:22:42 來源:億速云 閱讀:203 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細介紹“Golang怎么使用gob實現(xiàn)結(jié)構(gòu)體的序列化”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“Golang怎么使用gob實現(xiàn)結(jié)構(gòu)體的序列化”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

Golang有自己的序列化格式,稱為gob。使用gob可以對結(jié)構(gòu)進行編碼和解碼。你可以使用其他格式,如JSON, XML, protobuff等,具體選擇要根據(jù)實際需求,但當接收和發(fā)送都為Golang,我建議使用Go的gob格式。

Gob簡介

gob在kg/encoding/gob包中:

  • gob流是自描述的,這意味著我們不需要創(chuàng)建單獨的文件來解釋(使用protobuff格式需要創(chuàng)建文件)

  • gob流中的每個數(shù)據(jù)項之前都有其類型說明(一些預定義的類型)

Gob包很簡單,僅包括8個函數(shù)和5個類型:

func Register(value interface{})
func RegisterName(name string, value interface{})
type CommonType
type Decoder
func NewDecoder(r io.Reader) *Decoder
func (dec *Decoder) Decode(e interface{}) error
func (dec *Decoder) DecodeValue(v reflect.Value) error
type Encoder
func NewEncoder(w io.Writer) *Encoder
func (enc *Encoder) Encode(e interface{}) error
func (enc *Encoder) EncodeValue(value reflect.Value) error
type GobDecoder
type GobEncoder

單個對象序列化

首先定義student結(jié)構(gòu)體,包括兩個字段Name和Age.

使用gob.NewEncoder和gob.NewDecoder方法,接收io.Writer 和 io.Reader 對象,用于讀寫文件:

package main
import (
       "fmt"
       "os"
       "encoding/gob"
)
type Student struct {
       Name string
       Age int32
}
func main() {
       fmt.Println("Gob Example")
       student := Student{"Ketan Parmar",35}
       err := writeGob("./student.gob",student)
       if err != nil{
              fmt.Println(err)
       }
       var studentRead = new (Student)
       err = readGob("./student.gob",studentRead)
       if err != nil {
              fmt.Println(err)
       } else {
              fmt.Println(studentRead.Name, "\t", studentRead.Age)
       }
}
func writeGob(filePath string,object interface{}) error {
       file, err := os.Create(filePath)
       if err == nil {
              encoder := gob.NewEncoder(file)
              encoder.Encode(object)
       }
       file.Close()
       return err
}
func readGob(filePath string,object interface{}) error {
       file, err := os.Open(filePath)
       if err == nil {
              decoder := gob.NewDecoder(file)
              err = decoder.Decode(object)
       }
       file.Close()
       return err
}

列表數(shù)據(jù)序列化

首先創(chuàng)建student結(jié)構(gòu)體數(shù)組或slice,然后填充數(shù)據(jù)。下面示例無需修改readGob和writeGob函數(shù):

package main
import (
       "fmt"
       "os"
       "encoding/gob"
)
type Student struct {
       Name string
       Age int32
}
type Students []Student
func main() {
       fmt.Println("Gob Example")
       students := Students{}
       students = append(students,Student{"Student 1",20})
       students = append(students,Student{"Student 2",25})
       students = append(students,Student{"Student 3",30})
       err := writeGob("./student.gob",students)
       if err != nil{
              fmt.Println(err)
       }
       var studentRead = new (Students)
       err = readGob("./student.gob",studentRead)
       if err != nil {
              fmt.Println(err)
       } else {
              for _,v := range *studentRead{
                     fmt.Println(v.Name, "\t", v.Age)
              }
       }
}

上面兩個示例主要使用了NewEncoder 和 NewDecoder,接下來看看其他函數(shù):Register, Encode, EncodeValue, Decode 和 DecodeValue。

Encode 和 Decode 函數(shù)主要用于網(wǎng)絡應用程序,方法簽名如下:

func (dec *Decoder) Decode(e interface{}) error
func (enc *Encoder) Encode(e interface{}) error

簡單編碼示例

package main
import (
   "fmt"
   "encoding/gob"
   "bytes"
)
type Student struct {
   Name string
   Age int32
}
func main() {
   fmt.Println("Gob Example")
   studentEncode := Student{Name:"Ketan",Age:30}
   var b bytes.Buffer
   e := gob.NewEncoder(&b)
   if err := e.Encode(studentEncode); err != nil {
      panic(err)
   }
   fmt.Println("Encoded Struct ", b)
   var studentDecode Student
   d := gob.NewDecoder(&b)
   if err := d.Decode(&studentDecode); err != nil {
      panic(err)
   }
   fmt.Println("Decoded Struct ", studentDecode.Name,"\t",studentDecode.Age)
}

上面示例把student結(jié)構(gòu)序列化、反序列化。序列化后存儲在字節(jié)buffer變量b中,先可以使用b在網(wǎng)絡中傳輸。要解碼僅需要創(chuàng)建相同結(jié)構(gòu)對象并提供其地址。studentDecode變量獲得解碼的內(nèi)容。

編碼在TCP連接中使用

TCP客戶端:打開連接使用gob.Encoder方法編碼數(shù)據(jù)進行傳輸:

package main
import (
   "fmt"
   "encoding/gob"
   "net"
   "log"
)
type Student struct {
   Name string
   Age int32
}
func main() {
   fmt.Println("Client")
   //create structure object
    studentEncode := Student{Name:"Ketan",Age:30}
   fmt.Println("start client");
   // dial TCP connection
   conn, err := net.Dial("tcp", "localhost:8080")
   if err != nil {
      log.Fatal("Connection error", err)
   }
   //Create encoder object, We are passing connection object in Encoder
   encoder := gob.NewEncoder(conn)
   // Encode Structure, IT will pass student object over TCP connection
   encoder.Encode(studentEncode)
   // close connection
   conn.Close()
   fmt.Println("done");
}

TCP Server: 監(jiān)聽8080端口,在go協(xié)程中處理所有客戶端,使用gob.Decoder方法解碼student結(jié)構(gòu)體并輸出:

package main
import (
   "fmt"
   "net"
   "encoding/gob"
)
type Student struct {
   Name string
   Age int32
}
func handleConnection(conn net.Conn) {
   // create new decoder object and provide connection
   dec := gob.NewDecoder(conn)
   // create blank student object
   p := &Student{}
   // decode serialize data
   dec.Decode(p)
   // print
   fmt.Println("Hello ",p.Name,", Your Age is ",p.Age);
   // close connection for that client
   conn.Close()
}
func main() {
   fmt.Println("Server")
   // start TCP server and listen on port 8080
   ln, err := net.Listen("tcp", ":8080")
   if err != nil {
      // handle error
      panic(err)
   }
   for {
      // this blocks until connection or error
      conn, err := ln.Accept()
      if err != nil {
         // handle error
         continue
      }
      // a goroutine handles conn so that the loop can accept other connections
      go handleConnection(conn)
   }
}

上文中沒有實現(xiàn)序列化,本文給出golang-ttl-map實現(xiàn):

func (h *Heap) append(data Data) (err error) {
	h.fileMx.Lock()
	defer h.fileMx.Unlock()
       // 打開文件
	var file *os.File
	file, err = os.OpenFile(h.filePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
	if err != nil {
		return
	}
	defer func() {
		_ = file.Sync()
	}()
	defer func() {
		_ = file.Close()
	}()
       // 定義buffer
	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
       // 序列化
	err = enc.Encode(data)
	if err != nil {
		return
	}
	bs := buf.Bytes()
	bs = append(bs, '\n')
       // 寫入文件
	_, err = file.Write(bs)
	if err != nil {
		return
	}
	return
}

讀到這里,這篇“Golang怎么使用gob實現(xiàn)結(jié)構(gòu)體的序列化”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI