Golang使用gob实现结构体的序列化过程详解

 更新时间:2023年03月08日 08:30:04   作者:梦想画家  
Golang struct类型数据序列化用于网络传输数据或在磁盘上写入数据。在分布式系统中,一端生成数据、然后序列化、压缩和发送;在另一端,接收数据、然后解压缩、反序列化和处理数据,整个过程必须快速有效

Golang有自己的序列化格式,称为gob。使用gob可以对结构进行编码和解码。你可以使用其他格式,如JSON, XML, protobuff等,具体选择要根据实际需求,但当接收和发送都为Golang,我建议使用Go的gob格式。

Gob简介

gob在kg/encoding/gob包中:

  • gob流是自描述的,这意味着我们不需要创建单独的文件来解释(使用protobuff格式需要创建文件)
  • gob流中的每个数据项之前都有其类型说明(一些预定义的类型)

Gob包很简单,仅包括8个函数和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结构体,包括两个字段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
}

列表数据序列化

首先创建student结构体数组或slice,然后填充数据。下面示例无需修改readGob和writeGob函数:

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,接下来看看其他函数:Register, Encode, EncodeValue, Decode 和 DecodeValue。

Encode 和 Decode 函数主要用于网络应用程序,方法签名如下:

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结构序列化、反序列化。序列化后存储在字节buffer变量b中,先可以使用b在网络中传输。要解码仅需要创建相同结构对象并提供其地址。studentDecode变量获得解码的内容。

编码在TCP连接中使用

TCP客户端:打开连接使用gob.Encoder方法编码数据进行传输:

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: 监听8080端口,在go协程中处理所有客户端,使用gob.Decoder方法解码student结构体并输出:

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)
   }
}

上文中没有实现序列化,本文给出golang-ttl-map实现:

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实现结构体的序列化过程详解的文章就介绍到这了,更多相关Golang结构体序列化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Go语言HTTP服务实现GET和POST请求的同时支持

    Go语言HTTP服务实现GET和POST请求的同时支持

    做第三方接口有时需要用Get或者Post请求访问,本文主要介绍了Golang发送Get和Post请求的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-05-05
  • Golang实现简易的命令行功能

    Golang实现简易的命令行功能

    这篇文章主要为大家详细介绍了如何通过Golang实现一个简易的命令行功能,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的可以了解一下
    2023-02-02
  • Golang使用channel实现一个优雅退出功能

    Golang使用channel实现一个优雅退出功能

    最近补 Golang channel 方面八股的时候发现用 channel 实现一个优雅退出功能好像不是很难,之前写的 HTTP 框架刚好也不支持优雅退出功能,于是就参考了 Hertz 优雅退出方面的代码,为我的 PIANO 补足了这个 feature
    2023-03-03
  • 基于微服务框架go-micro开发gRPC应用程序

    基于微服务框架go-micro开发gRPC应用程序

    这篇文章介绍了基于微服务框架go-micro开发gRPC应用程序的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • Go与C语言的互操作实现

    Go与C语言的互操作实现

    在Go与C语言互操作方面,Go更是提供了强大的支持。尤其是在Go中使用C,你甚至可以直接在Go源文件中编写C代码,本文就详细的介绍一下如何使用,感兴趣的可以了解一下
    2021-12-12
  • golang 如何获取pem格式RSA公私钥长度

    golang 如何获取pem格式RSA公私钥长度

    这篇文章主要介绍了golang 如何获取pem格式RSA公私钥长度操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • 如何使用腾讯云go sdk 查询对象存储中最新文件

    如何使用腾讯云go sdk 查询对象存储中最新文件

    这篇文章主要介绍了使用腾讯云go sdk 查询对象存储中最新文件,这包括如何创建COS客户端,如何逐页检索对象列表,并如何对结果排序以找到最后更新的对象,我们还展示了如何优化用户体验,通过实时进度更新和检索多个文件来改进程序,需要的朋友可以参考下
    2024-03-03
  • go语言里包的用法实例

    go语言里包的用法实例

    这篇文章主要介绍了go语言里包的用法,实例分析了Go语言里包的原理与使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • Golang实现Biginteger大数计算实例详解

    Golang实现Biginteger大数计算实例详解

    这篇文章主要为大家介绍了Golang实现Biginteger大数计算实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Go语言中iota的具体使用

    Go语言中iota的具体使用

    Go语言中,iota是一个用于生成一系列相关常量值的常量生成器,常应用于枚举、位操作等场景,广泛用于定义HTTP状态码、权限控制等,本文就来介绍一下iota的具体使用,感兴趣的可以了解一下
    2024-11-11

最新评论