FAQ
Hello,

I need to build a []map[string]interface{} from sql.Rows, but I can't
figure out how to do it. I came across this question on StackOverflow
(http://stackoverflow.com/questions/17840963/create-a-map-in-golang-from-database-rows),
but none of the answers are satisfactory (it's also a dated question). I
wanted to check to see if anyone on this list had any better solutions,
preferably using the standard library if it's not overly complex.

Thanks,
Craig

--
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/d/optout.

Search Discussions

  • Kyle Wolfe at Nov 30, 2015 at 3:37 pm
    Craig,

    If you know what the table structure is ahead, it's trivial to do a normal
    scan and then return a map[string]interface{} form your scan. Beccause of
    this, I'm assuming that you have a situation where you're unaware of the
    table structure at runtime and your doing a "select *" all the time? If
    this is the case, databse/sql is not set up for this use case from what I
    see in the do, as driver.Value is not exported
    (https://golang.org/src/database/sql/sql.go#L1615). With that said, I agree
    with the first response in that SO. Use sqlx
    (https://godoc.org/github.com/jmoiron/sqlx#Row.MapScan)
    On Monday, November 30, 2015 at 10:10:06 AM UTC-5, Craig Weber wrote:

    Hello,

    I need to build a []map[string]interface{} from sql.Rows, but I can't
    figure out how to do it. I came across this question on StackOverflow (
    http://stackoverflow.com/questions/17840963/create-a-map-in-golang-from-database-rows),
    but none of the answers are satisfactory (it's also a dated question). I
    wanted to check to see if anyone on this list had any better solutions,
    preferably using the standard library if it's not overly complex.

    Thanks,
    Craig
    --
    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/d/optout.
  • C Banning at Nov 30, 2015 at 5:33 pm
    Get a Rows value, 'r', with the db.Query(). r.Columns has the map keys in
    sequence - 'cols'; r.Next() will get you a row; and r.Scan() will walk
    through the values for the map keys in sequence of cols[i].
    On Monday, November 30, 2015 at 9:10:06 AM UTC-6, Craig Weber wrote:

    Hello,

    I need to build a []map[string]interface{} from sql.Rows, but I can't
    figure out how to do it. I came across this question on StackOverflow (
    http://stackoverflow.com/questions/17840963/create-a-map-in-golang-from-database-rows),
    but none of the answers are satisfactory (it's also a dated question). I
    wanted to check to see if anyone on this list had any better solutions,
    preferably using the standard library if it's not overly complex.

    Thanks,
    Craig
    --
    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/d/optout.
  • Daniel Theophanes at Nov 30, 2015 at 8:28 pm
    Look
    at https://bitbucket.org/kardianos/table/src/default/tablebuffer.go?fileviewer=file-view-default#tablebuffer.go-88

    from https://godoc.org/bitbucket.org/kardianos/table which buffers the
    results of any query.
    -Daniel

    On Monday, November 30, 2015 at 7:10:06 AM UTC-8, Craig Weber wrote:

    Hello,

    I need to build a []map[string]interface{} from sql.Rows, but I can't
    figure out how to do it. I came across this question on StackOverflow (
    http://stackoverflow.com/questions/17840963/create-a-map-in-golang-from-database-rows),
    but none of the answers are satisfactory (it's also a dated question). I
    wanted to check to see if anyone on this list had any better solutions,
    preferably using the standard library if it's not overly complex.

    Thanks,
    Craig
    --
    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/d/optout.
  • Djadala at Dec 1, 2015 at 4:15 am
    see this file:
    https://github.com/mattn/go-oci8/blob/master/sqlrows_test.go

    it is usable as independent package.

    Djadala
    On Monday, November 30, 2015 at 5:10:06 PM UTC+2, Craig Weber wrote:

    Hello,

    I need to build a []map[string]interface{} from sql.Rows, but I can't
    figure out how to do it. I came across this question on StackOverflow (
    http://stackoverflow.com/questions/17840963/create-a-map-in-golang-from-database-rows),
    but none of the answers are satisfactory (it's also a dated question). I
    wanted to check to see if anyone on this list had any better solutions,
    preferably using the standard library if it's not overly complex.

    Thanks,
    Craig
    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 30, '15 at 3:09p
activeDec 1, '15 at 4:15a
posts5
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase