Home > Article > Backend Development > golang interface to map
In the Go language, the interface type is usually used to represent that a certain object may have multiple different implementations, so that code can be written more flexibly, and it also facilitates code maintenance and expansion. But in some cases, we need to convert the interface to map type. This article will introduce how to implement the method of converting interface to map in golang.
Why it is necessary to convert interface to map
During the development process, sometimes it is necessary to convert interface type data to map type. Common situations include the following:
Suppose we have a structure named Person. The entity contains three attributes: name, age, and gender. Now we need to get the name and age data from a Person type interface object. This can be achieved by converting the interface to a map type:
type Person struct { Name string Age int Gender string } func main() { p := Person{"Tom", 20, "male"} data := map[string]interface{}{"name": p.Name, "age": p.Age, "gender": p.Gender} fmt.Println(data) }
When serializing a structure into JSON or other formats, it is generally converted into a map type before operation. At this time, converting the interface type to the map type is also a very important step.
How to convert interface to map
In golang, you can use assertion and reflection to convert interface type to map type.
Use assertion method to convert interface to map[string]interface{} type, the implementation is as follows:
func interfaceToMapByAssertion(iface interface{}) (map[string]interface{}, bool) { t := reflect.TypeOf(iface) if t.Kind() == reflect.Ptr { t = t.Elem() } if t.Kind() != reflect.Struct { return nil, false } v := reflect.ValueOf(iface) if v.Kind() == reflect.Ptr { v = v.Elem() } fields := make(map[string]interface{}) for i := 0; i < t.NumField(); i++ { field := t.Field(i) fieldValue := v.Field(i).Interface() fields[field.Name] = fieldValue } return fields, true }
This function determines whether it is a structure type by obtaining the type information of the interface, then traverses the fields in the structure one by one, obtains the value of each field through reflection and stores it in the map. If the conversion is successful, the converted map and the conversion result are returned as true, otherwise nil and false are returned.
Use reflection method to convert interface can be converted into two types: map[string]interface{} and map[string]reflect.Value type of map.
The code to convert interface to map[string]interface{} is as follows:
func interfaceToMapByReflection(v interface{}) map[string]interface{} { result := make(map[string]interface{}) val := reflect.ValueOf(v) for i := 0; i < val.NumField(); i++ { fieldName := val.Type().Field(i).Name fieldValue := val.Field(i).Interface() result[fieldName] = fieldValue } return result }
This function uses the Type and Value obtained through reflection to traverse the structure. All fields, get their names and values, and store them in the result as key-value pairs of type map[string]interface{}.
The code to convert interface to map[string]reflect.Value type is as follows:
func interfaceToMapByReflectionWithReflectValue(v interface{}, res map[string]reflect.Value) { val := reflect.ValueOf(v) for i := 0; i < val.NumField(); i++ { fieldName := val.Type().Field(i).Name fieldValue := val.Field(i) res[fieldName] = fieldValue } }
The implementation of this function is similar to the above function, except that it stores the result as map [string]reflect.Value type, this type can obtain the type information of the stored value when the program is running.
Summary
This article introduces two ways to convert the interface type to the map type in golang: using assertions and using reflection. Both methods can convert the interface into a map, but reflection can be used to obtain the type information of the stored value. When using these methods, we need to pay attention to determine whether the type of interface is a structure and handle it accordingly.
The above is the detailed content of golang interface to map. For more information, please follow other related articles on the PHP Chinese website!