```
package test
import (
"errors"
"fmt"
"reflect"
"strings"
)
func MapToStruct() {
mList := []map[string]interface{}{
{"Id": 213, "Name": "zhaoliu", "Sex": "男"},
{"Id": 56, "Name": "zhangsan", "Sex": "男"},
{"Id": 7, "Name": "lisi", "Sex": "女"},
{"Id": 978, "Name": "wangwu", "Sex": "男"},
}
type User struct {
Id int
Name string
Sex string
}
users := []*User{}
mapToStruct(mList, &users)
fmt.Printf("users: %+v\n", users)
}
func mapToStruct(mList []map[string]interface{}, model interface{}) (err error) {
val := reflect.Indirect(reflect.ValueOf(model))
typ := val.Type()
fmt.Printf("val: %v\n", val)
fmt.Printf("typ: %v\n", typ)
for _, r := range mList {
mVal := reflect.Indirect(reflect.New(typ.Elem().Elem())).Addr()
//fmt.Printf("mVal: %+v\n", mVal)
for key, val := range r {
err = setField(mVal.Interface(), key, val)
if err != nil {
return err
}
}
//fmt.Printf("mVal: %+v\n", mVal)
val = reflect.Append(val, mVal)
}
//echo: val: [0xc04206c2a0 0xc04206c390 0xc04206c480 0xc04206c570]
fmt.Printf("val: %+v\n", val.Interface())
model = val.Interface()
return err
}
//用map的值替换结构的值
func setField(obj interface{}, name string, value interface{}) error {
// 将首字母转换为大写
sl := strings.Split(name, "")
sl[0] = strings.ToUpper(sl[0])
name = strings.Join(sl, "")
structValue := reflect.ValueOf(obj).Elem() //结构体属性值
//fmt.Printf("structValue: %+v\n", structValue)
structFieldValue := structValue.FieldByName(name) //结构体单个属性值
//fmt.Printf("structFieldValue: %+v\n", structFieldValue)
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
}
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
}
structFieldType := structFieldValue.Type() //结构体的类型
val := reflect.ValueOf(value) //map值的反射值
if structFieldType != val.Type() {
return errors.New("type is err")
}
structFieldValue.Set(val)
return nil
}
```
更多评论
```
package main
import (
"errors"
"fmt"
"reflect"
"strings"
)
type User struct {
Id int
Name string
Sex string
}
var users []*User
func main() {
mList := []map[string]interface{}{
{"Id": 213, "Name": "zhaoliu", "Sex": "男"},
{"Id": 56, "Name": "zhangsan", "Sex": "男"},
{"Id": 7, "Name": "lisi", "Sex": "女"},
{"Id": 978, "Name": "wangwu", "Sex": "男"},
}
mapToStruct(mList, &users)
fmt.Printf("users: %+v\n", users[0])
}
func mapToStruct(mList []map[string]interface{}, model interface{}) (err error) {
val := reflect.Indirect(reflect.ValueOf(model))
typ := val.Type()
for _, r := range mList {
mVal := reflect.Indirect(reflect.New(typ.Elem().Elem())).Addr()
for key, val := range r {
err = setField(mVal.Interface(), key, val)
if err != nil {
return err
}
}
v, ok := mVal.Interface().(*User)
if ok {
users = append(users, v)
}
}
return err
}
//用map的值替换结构的值
func setField(obj interface{}, name string, value interface{}) error {
// 将首字母转换为大写
sl := strings.Split(name, "")
sl[0] = strings.ToUpper(sl[0])
name = strings.Join(sl, "")
structValue := reflect.ValueOf(obj).Elem() //结构体属性值
//fmt.Printf("structValue: %+v\n", structValue)
structFieldValue := structValue.FieldByName(name) //结构体单个属性值
//fmt.Printf("structFieldValue: %+v\n", structFieldValue)
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
}
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
}
structFieldType := structFieldValue.Type() //结构体的类型
val := reflect.ValueOf(value) //map值的反射值
if structFieldType != val.Type() {
return errors.New("type is err")
}
structFieldValue.Set(val)
return nil
}
```
#1