On Sunday, April 15, 2012 7:13:35 PM UTC+2, Peter Kleiweg wrote:
I wrote this function to print the results from a query with
database/sql. The function doesn't now how many columns there
are. All it nows is that all columns are of type string.
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/
--I wrote this function to print the results from a query with
database/sql. The function doesn't now how many columns there
are. All it nows is that all columns are of type string.
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/
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.