Home  >  Article  >  Backend Development  >  Go language document interpretation: Detailed explanation of encoding/json.MarshalIndent function

Go language document interpretation: Detailed explanation of encoding/json.MarshalIndent function

WBOY
WBOYOriginal
2023-11-03 15:48:35752browse

Go language document interpretation: Detailed explanation of encoding/json.MarshalIndent function

Go language document interpretation: Detailed explanation of encoding/json.MarshalIndent function

1. Function introduction
encoding/json.MarshalIndent function is used in Go language to Function to convert data structure into JSON format. It is defined as follows:

func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)

This function accepts three parameters, namely v interface{} , prefix string, indent string. The parameter v represents the data structure to be converted to JSON format, and prefix and indent represent the prefix and indent of each line in the generated JSON string respectively.

2. Function Example
Below we use a specific example to explain the usage of the encoding/json.MarshalIndent function. Suppose we have a structure Person as follows:

type Person struct {

Name string `json:"name"`
Age  int    `json:"age"`

}

We can use the encoding/json.MarshalIndent function to convert the structure into JSON format string and output it. The specific code is as follows:

package main

import (

"encoding/json"
"fmt"

)

type Person struct {

Name string `json:"name"`
Age  int    `json:"age"`

}

func main() {

p := Person{Name: "张三", Age: 20}
b, err := json.MarshalIndent(p, "", "    ")
if err != nil {
    fmt.Println("转换失败:", err)
    return
}
fmt.Println(string(b))

}

In the above code, we first define a Person structure and initialize a Person object p. Then, we call the encoding/json.MarshalIndent function to convert p to a JSON-formatted string and use 4 spaces for indentation. Finally, we output the generated JSON string to the console.

3. Function return value
encoding/json.MarshalIndent function returns two values, which are []byte and error. []byte represents the converted JSON format string, and error represents whether an error occurred during the conversion process. In the above example code, we use string(b) to convert the return value of []byte type to string type and output it to the console.

4. Summary
encoding/json.MarshalIndent function is an important function in Go language for converting data structures into JSON format. By setting the prefix and indentation parameters, we can control the format of the generated JSON-formatted string. When using this function, you need to note that the data structure passed in must conform to the JSON specification, otherwise a conversion failure error may occur.

The above is a detailed explanation of the encoding/json.MarshalIndent function. Through the introduction of this article, you should have a deeper understanding of the usage of this function. In the actual programming process, I hope you can flexibly use this function to convert between data structure and JSON format.

The above is the detailed content of Go language document interpretation: Detailed explanation of encoding/json.MarshalIndent function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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