FAQ
Oh, that's quite elegant.

Wish you didn't always get byte[] back from Scan() though; eg. if
http://play.golang.org/p/jxza3pbqq9 could print out the actual types, you
could use the switch statement to cast the interface value to the
appropriate type and return a strongly typed map[string]interface{}.

Alas, that doesn't seem to be possible~

~
Doug.
On Monday, April 15, 2013 4:43:48 PM UTC+8, Julien Schmidt wrote:

http://play.golang.org/p/njPBsg0JjD
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/
--
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 6 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 15, '13 at 8:43a
activeApr 17, '13 at 10:36p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase