Several encoding and decoding methods commonly used in golang

PHPz
Release: 2023-04-03 09:40:30
Original
1818 people have browsed it

When we need to convert one encoding format to another encoding format, we need to use transcoding. In the Go language, the transcoding operation is very convenient. You can use the built-in encoding package to quickly complete the transcoding operation.

The encoding package in Go language provides many commonly used encoding and decoding methods, such as JSON, XML, Base64, Gob, etc. It also provides character set (charset) support, which can be used to solve Chinese garbled characters, etc. Special character issue.

Let’s introduce several commonly used transcoding operations:

1. Character set conversion

In Go language, we can use golang.org/x/ The simplifiedchinese and traditionalchinese sub-packages in the text/encoding package are used to convert simplified and traditional Chinese to each other. For example, we can convert Traditional Chinese to Simplified Chinese:

import "golang.org/x/text/encoding/simplifiedchinese"
import "golang.org/x/text/transform"

func main() {
    // 将繁体中文转换为简体中文
    str := "測試"
    input := []byte(str)
    output := make([]byte, len(str))
    tr := simplifiedchinese.GBK.NewDecoder()
    _, _, err := tr.Transform(output, input, true)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(output))
}
Copy after login

In the code, we first introduced the encoding/simplifiedchinese package, and used the NewDecoder() method of the GBK character set to create a decoder object, and then passed Transform () method converts Traditional Chinese str to Simplified Chinese.

2. Base64 encoding

Base64 encoding is a method of encoding any binary data into ASCII characters. In Go language, we can use the encoding/base64 package to perform Base64 encoding and decoding operations. For example, we can convert a string to Base64 encoding:

import "encoding/base64"

func main() {
    // 将字符串转换为Base64编码
    str := "hello, world"
    base64Str := base64.StdEncoding.EncodeToString([]byte(str))
    fmt.Println(base64Str)
}
Copy after login

In the code, we use the EncodeToString() method of base64.StdEncoding to convert the string str to Base64 encoding by introducing the encoding/base64 package.

3. JSON encoding

JavaScript Object Notation (JSON) is a lightweight data exchange format. In Go language, we can use the encoding/json package to perform JSON encoding and decoding operation. For example, we create a structure object, then encode it into JSON format and output it to the console:

import "encoding/json"

type User struct {
    Name string `json:"name"`
    Age int `json:"age"`
    Gender string `json:"gender"`
}

func main() {
    // 编码结构体为JSON格式
    user := User{"Tom", 20, "male"}
    data, _ := json.Marshal(user)
    fmt.Println(string(data))
}
Copy after login

In the code, we define a User structure and use the json.Marshal() method to The structure is encoded into JSON format, and the encoded result is output to the console.

4. XML encoding

eXtensible Markup Language (XML) is an extensible markup language. In Go language, we can use the encoding/xml package to perform XML encoding and decoding operations. For example, we create a structure object, then encode it into XML format and output it to the console:

import "encoding/xml"

type User struct {
    Name string `xml:"name"`
    Age int `xml:"age"`
    Gender string `xml:"gender"`
}

func main() {
    // 编码结构体为XML格式
    user := User{"Tom", 20, "male"}
    data, _ := xml.Marshal(user)
    fmt.Println(string(data))
}
Copy after login

In the code, we define a User structure and use the xml.Marshal() method to It is encoded in XML format, and finally the encoded result is output to the console.

The above are several commonly used transcoding operations. Through these methods, we can easily complete operations such as encoding conversion, encryption and decryption, and realize data interoperability between different formats and encodings.

The above is the detailed content of Several encoding and decoding methods commonly used in golang. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!