Convert golang structure to byte

WBOY
Release: 2023-05-10 22:54:36
Original
1608 people have browsed it

In programming, serialization and deserialization of data are very common tasks. In Golang, we can use the "encoding/binary" package in the standard library to convert data structures into byte sequences, which is particularly useful in data transmission and storage. This article will introduce how to convert a structure into a sequence of bytes using Golang.

1. Structure in Golang

In Golang, a structure is a user-defined data structure that allows us to combine different data types together to form a More complex data types. The syntax for defining a structure is as follows:

type 结构体名 struct {
  字段名1 数据类型1
  字段名2 数据类型2
  ……
}
Copy after login

For example, we can define a Person structure to represent a person's information:

type Person struct {
  Name string
  Age int
  Height float64
}
Copy after login

2. Byte sequence in Golang
In Golang In , you can use byte slices to represent byte sequences, which can serialize a data structure into a byte sequence for transmission or storage in a file. For example, we can use the following statement to create an empty byte slice:

b := make([]byte, 0)
Copy after login

3. Convert the structure to a byte sequence

The "encoding/binary" package in Golang provides a set of Function for converting Golang data types to byte sequences. For structs, we can use the "binary.Write" function to convert it into a sequence of bytes. The following is an example:

package main

import (
  "encoding/binary"
  "fmt"
)

type Person struct {
  Name string
  Age int
  Height float64
}

func main() {
  p := Person{"Tom", 20, 1.8}
  b := make([]byte, 0)

  binary.Write(&b, binary.LittleEndian, p)

  fmt.Println(b)
}
Copy after login

In this example, we create a Person structure, serialize it into byte slices using binary mode, and then output the byte slices. We can see that the output result is:

[3 84 111 109 20 0 0 0 0 0 0 0 0 0 0 0 236 81 184 30 133 235 64]
Copy after login

The output result is actually the result of converting "Tom", "20" and "1.8" into a byte sequence. Binary mode is used here, and little-endian mode is used (that is, the low byte is stored at the starting position).

We can see that the generated byte sequence contains the field name and type information of the structure, because this information needs to be known during deserialization to correctly restore the data structure.

4. Convert the byte sequence into a structure

In order to deserialize the byte sequence into a structure, we can use the "binary.Read" function. Here is an example:

package main

import (
  "bytes"
  "encoding/binary"
  "fmt"
)

type Person struct {
  Name   string
  Age    int
  Height float64
}

func main() {
  p := Person{"Tom", 20, 1.8}
  b := make([]byte, 0)

  binary.Write(&b, binary.LittleEndian, p)

  out := new(Person)
  buf := bytes.NewReader(b)
  binary.Read(buf, binary.LittleEndian, out)

  fmt.Printf("%+v", *out)
}
Copy after login

In this example, we first use "binary.Write" to serialize the Person structure into a byte slice. We then used "bytes.NewReader" to slice the bytes into a buffer. Finally, we use the "binary.Read" function to read the data in the buffer into a new Person structure and output the result.

In this example, the output is:

{Name:"Tom", Age:20, Height:1.8}
Copy after login

This indicates that we successfully deserialized the byte sequence into a Person structure.

Summary

In Golang, you can use the "encoding/binary" package to convert data structures into byte sequences. This is very useful because it allows us to transfer data to other computers or store it in files. To convert a structure into a sequence of bytes, we can use the "binary.Write" function, and to deserialize a sequence of bytes into a structure, we can use the "binary.Read" function. These functions can handle different data types and support different endiannesses.

I hope this article will be helpful to convert a structure into a byte sequence in Golang.

The above is the detailed content of Convert golang structure to byte. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template