golang struct轉map
阿新 • • 發佈:2017-09-23
link data eof ref for pac cpp main ring
struct轉map
package main import ( "fmt" "reflect" "time" ) type User struct { Id int64 Username string Password string Logintime time.Time } 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[t.Field(i).Name] = v.Field(i).Interface() } return data } func main() { user := User{5, "zhangsan", "pwd", time.Now()} data := Struct2Map(user) fmt.Println(data) }
golang struct轉map