Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to serialize binary data through Gob package in Go

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Go how to serialize binary data through the Gob package, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

The following shows you how to serialize data to text files based on JSON and CSV formats. In addition, Go officially provides encoding/gob packages to serialize data into binary streams for transmission over the network.

We have introduced the basic use of the Gob package as a binary data codec tool in the previous Go introductory tutorial. Here is a brief demonstration of how to write Gob-encoded binary data to a disk file:

Package main

Import (

"bytes"

"encoding/gob"

"fmt"

"io/ioutil"

)

Type Article struct {

Id int

Title string

Content string

Author string

}

/ / write binary data to disk file

Func write (data interface {}, filename string) {

Buffer: = new (bytes.Buffer)

Encoder: = gob.NewEncoder (buffer)

Err: = encoder.Encode (data)

If err! = nil {

Panic (err)

}

Err = ioutil.WriteFile (filename, buffer.Bytes (), 0600)

If err! = nil {

Panic (err)

}

}

/ / load binary data from a disk file

Func read (data interface {}, filename string) {

Raw, err: = ioutil.ReadFile (filename)

If err! = nil {

Panic (err)

}

Buffer: = bytes.NewBuffer (raw)

Dec: = gob.NewDecoder (buffer)

Err = dec.Decode (data)

If err! = nil {

Panic (err)

}

}

Func main () {

Article: = Article {

Id: 1

Title: encoding and Decoding binary data based on Gob package

Content: "serialize binary data through Gob packets for transmission over the network"

Author: "King of the Academy"

}

Write (article, "article_data")

Var articleData Article

Read (& articleData, "article_data")

Fmt.Printf ("% # v\ n", articleData)

}

By running the above code, the binary data can be decoded normally, which means that the Gob package is encoded and decoded successfully:

You can also see the generated article_data file in the current directory, which contains encoded data in binary format:

What is gogo is the abbreviation of golang? golang is a static strongly typed, compiled, concurrent and garbage-collected programming language developed by Google. Its syntax is similar to C language, but it does not include functions such as enumeration, exception handling, inheritance, generics, assertions, virtual functions and so on.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report