Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database/sql: permit tagged struct in Scanner #9347

Closed
matejkramny opened this issue Dec 16, 2014 · 3 comments
Closed

database/sql: permit tagged struct in Scanner #9347

matejkramny opened this issue Dec 16, 2014 · 3 comments

Comments

@matejkramny
Copy link

Hi all,

First of all, thank you for Go. Its Awesome!

I like the idea how encoding/json Marshals and Unmarshals json objects into structs..

One could define the database model like so:

type UserModel struct {
    Id int `db:"id"`
    Name string `db:"user_name"`
}

And when scanning rows from the database:

for rows.Next() {
    user := UserModel{}
    if err := rows.Scan(&user); err != nil {
        panic(err)
    }
}

What are your thoughts?

@ianlancetaylor ianlancetaylor changed the title database/sql rows.Scan a struct database/sql: permit tagged struct in Scanner Dec 16, 2014
@pbnjay
Copy link
Contributor

pbnjay commented Dec 17, 2014

https://github.com/jmoiron/sqlx does this quite well already with fairly minimal overhead, I highly recommend it. (You can even use named parameters in queries using the tags).

@rsc
Copy link
Contributor

rsc commented Apr 10, 2015

3rd-party packages to make nicer scanners seem like the best route here. I don't see a compelling reason to try to standardize this in database/sql itself, and worse I don't see one obvious design. There are many possibilities. Let people use what works for them instead of forcing one.

@rsc rsc closed this as completed Apr 10, 2015
@mattn
Copy link
Member

mattn commented Apr 10, 2015

I'm thinking, time.Time should have simple scanner. Because when driver can't recognize type of the field value, driver must return string or []byte.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants