Go 语言的 ORM 框架是否支持动态表结构操作?

beegcom · · 939 次点击
```golang package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" ) func main() { db, err := gorm.Open("mysql", "root:root@/test?charset=utf8&parseTime=True&loc=Local") if err != nil { panic("failed to connect database") } defer db.Close() rows, err := db.Table("users").Rows() // (*sql.Row) if err != nil { panic(err) } defer rows.Close() var results []map[string]interface{} cols, err := rows.Columns() if err != nil { panic(err) } for rows.Next() { var row = make([]interface{}, len(cols)) var rowp = make([]interface{}, len(cols)) for i := 0; i < len(cols); i++ { rowp[i] = &row[i] } rows.Scan(rowp...) rowMap := make(map[string]interface{}) for i, col := range cols { rowMap[col] = row[i] } results = append(results, rowMap) fmt.Println(rowMap) } } ```
#1