Home > Backend Development > Golang > Common Golang serialization and deserialization techniques

Common Golang serialization and deserialization techniques

王林
Release: 2024-01-28 10:19:06
Original
660 people have browsed it

Common Golang serialization and deserialization techniques

Commonly used serialization and deserialization technologies in Golang require specific code examples

1. The concepts of serialization and deserialization

Serialization is the process of converting a data structure or object into a binary or other format so that it can be transmitted over a network or saved to persistent storage. Deserialization is to convert the serialized data back to the original data structure or object.

In Golang, serialization and deserialization are often used to handle the storage, transmission and communication of data, especially in scenarios that deal with interactions with external systems or other programming languages, such as database operations and network communications. wait.

2. Serialization and deserialization technology in Golang

Golang provides a variety of serialization and deserialization technologies, including JSON, XML, Gob, etc. Three of the commonly used serialization and deserialization techniques will be introduced below and corresponding code examples will be provided.

  1. JSON Serialization and Deserialization

JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and write. Golang has built-in support for JSON, making it easy to convert between objects and JSON formats.

Here is a sample code that demonstrates how to use Golang for JSON serialization and deserialization:

package main

import (
    "encoding/json"
    "fmt"
)

type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

func main() {
    // 序列化
    person := Person{
        Name: "Alice",
        Age:  20,
    }
    jsonBytes, err := json.Marshal(person)
    if err != nil {
        fmt.Println("序列化失败:", err)
        return
    }
    fmt.Println("序列化结果:", string(jsonBytes))

    // 反序列化
    var newPerson Person
    err = json.Unmarshal(jsonBytes, &newPerson)
    if err != nil {
        fmt.Println("反序列化失败:", err)
        return
    }
    fmt.Println("反序列化结果:", newPerson)
}
Copy after login
  1. XML serialization and deserialization

XML (eXtensible Markup Language) is a general markup language that is often used to represent and transmit structured data. Golang provides the xml package to support XML serialization and deserialization.

Here is a sample code that demonstrates how to use Golang for XML serialization and deserialization:

package main

import (
    "encoding/xml"
    "fmt"
)

type Person struct {
    Name string `xml:"name"`
    Age  int    `xml:"age"`
}

func main() {
    // 序列化
    person := Person{
        Name: "Alice",
        Age:  20,
    }
    xmlBytes, err := xml.Marshal(person)
    if err != nil {
        fmt.Println("序列化失败:", err)
        return
    }
    fmt.Println("序列化结果:", string(xmlBytes))

    // 反序列化
    var newPerson Person
    err = xml.Unmarshal(xmlBytes, &newPerson)
    if err != nil {
        fmt.Println("反序列化失败:", err)
        return
    }
    fmt.Println("反序列化结果:", newPerson)
}
Copy after login
  1. Gob serialization and deserialization

Gob is a binary serialization format unique to Golang, which has high encoding and decoding efficiency. Golang has built-in support for Gob, which enables efficient data exchange between Golang programs.

The following is a sample code that demonstrates how to use Golang for Gob serialization and deserialization:

package main

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

type Person struct {
    Name string
    Age  int
}

func main() {
    // 序列化
    person := Person{
        Name: "Alice",
        Age:  20,
    }
    var buf bytes.Buffer
    encoder := gob.NewEncoder(&buf)
    err := encoder.Encode(person)
    if err != nil {
        fmt.Println("序列化失败:", err)
        return
    }
    fmt.Println("序列化结果:", buf.Bytes())

    // 反序列化
    var newPerson Person
    decoder := gob.NewDecoder(&buf)
    err = decoder.Decode(&newPerson)
    if err != nil {
        fmt.Println("反序列化失败:", err)
        return
    }
    fmt.Println("反序列化结果:", newPerson)
}
Copy after login

The above is a code example of commonly used serialization and deserialization techniques in Golang. In actual development, choosing appropriate serialization and deserialization technologies according to specific needs and scenarios can effectively improve the readability, maintainability and performance of the code.

The above is the detailed content of Common Golang serialization and deserialization techniques. 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