Home  >  Article  >  How to convert struct in go language

How to convert struct in go language

DDD
DDDOriginal
2023-06-16 12:01:311385browse

Go language conversion struct: 1. Convert struct to map, using the "TypeOf" and "ValueOf" functions in the reflect package to obtain the type and value of the struct, and then traverse each field of the struct. And store it into a map; 2. Convert map to struct. The input parameters are a map and a pointer to the struct. The output is empty. This function maps the values ​​in the map to the corresponding fields in the struct through reflection.

How to convert struct in go language

The operating environment of this tutorial: Windows 10 system, go1.20 version, Dell g3 computer.

Go language is a statically typed programming language. One of its characteristics is to define and organize complex data types through struct. Converting struct is one of the basic skills that must be mastered in Go language development. This article will introduce how to convert struct.

Definition of struct:

In Go language, you can define struct in the following way:

type Person struct {
Name string
Age int
}

This definition represents a Person type struct, It has two fields: Name and Age.

For a defined struct, we can instantiate an object and set its value in the following ways:

a := Person{
Name: "Alice",
Age: 20,
}

In this way, we can easily set variables of the struct type Assignment and use.

1. Convert struct to map

When we need to convert a struct type to a map type, we can use the following code:

func StructToMap(obj interface{}) map[string]interface{} {
objType := reflect.TypeOf(obj)
objVal := reflect.ValueOf(obj)
data := make(map[string]interface{})
for i := 0; i < objVal.NumField(); i++ {
data[objType.Field(i).Name] = objVal.Field(i).Interface()
}
return data
}

The The input parameter of the function is an interface type, and the output is a map type. This function uses the TypeOf and ValueOf functions in the reflect package to obtain the type and value of the struct. The function then iterates through each field of the struct and stores it into a map.

2. Convert map to struct

When we need to convert a map type to a struct type, we can use the following code:

func MapToStruct(m map[string]interface{}, s interface{}) {
sType := reflect.TypeOf(s).Elem()
sVal := reflect.ValueOf(s).Elem()
for i := 0; i < sType.NumField(); i++ {
field := sType.Field(i)
val := reflect.ValueOf(m[field.Name])
sVal.Field(i).Set(val)
}
}

The The input parameters of the function are a map and a pointer to a struct, and the output is empty. This function maps the values ​​in the map to the corresponding fields in the struct through reflection.

Summary:

Through the above introduction, we can see that in Go language, converting struct is very simple and can be easily achieved by using reflection. Through the above method, we can happily use the struct type and convert it during the development process of Go language, so as to develop more efficiently

The above is the detailed content of How to convert struct in go language. 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