My question is, can this be done in a better or simpler way? I
am not sure if the use of new() is correct, I have never used
new() before. And I think *(fields[i].(*string)) looks a bit
convoluted.
func printTable(rows *sql.Rows) {
cols, _ := rows.Columns()
n := len(cols)
for i := 0; i < n; i++ {
fmt.Print(cols[i], "\t")
}
fmt.Println()
var fields []interface{}
for i := 0; i < n; i++ {
fields = append(fields, new(string))
}
for rows.Next() {
rows.Scan(fields...)
for i := 0; i < n; i++ {
fmt.Print(*(fields[i].(*string)), "\t")
}
fmt.Println()
}
}
--
Peter Kleiweg
http://pkleiweg.home.xs4all.nl/
int64 float64 bool []byte string [*] everywhere except from Rows.Next. time.TimeSource: http://golang.org/pkg/database/sql/driver/#Value