golang struct to map
func Struct2Map(obj interface{}) map[string]interface{} { t := reflect.TypeOf(obj) v := reflect.ValueOf(obj) var data = make(map[string]interface{}) for i := 0; i < t.NumField(); i++ { data[strings.ToLower(t.Field(i).Name)] = v.Field(i).Interface() } return data }
或者直接用开源库: "github.com/fatih/structs" 原文链接:https://www.f2er.com/go/189390.html