Created
September 28, 2021 08:58
-
-
Save wirelessr/58217cafbbe6a37ee61208ba240962fb to your computer and use it in GitHub Desktop.
Fine-tune Scan of database/sql to fetch dynamic columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Scan(list *sql.Rows) (rows []map[string]interface{}) { | |
fields, _ := list.Columns() | |
for list.Next() { | |
scans := make([]interface{}, len(fields)) | |
row := make(map[string]interface{}) | |
for i := range scans { | |
scans[i] = &scans[i] | |
} | |
list.Scan(scans...) | |
for i, v := range scans { | |
var value = "" | |
if v != nil { | |
value = fmt.Sprintf("%s", v) | |
} | |
row[fields[i]] = value | |
} | |
rows = append(rows, row) | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment