FWIW, things like `SELECT * FROM ...` are in my opinion a code smell: there are usually some (implicit) expectations on which columns will be returned in what order that'll break horribly if someone two departments removed decides "this table needs another column".

In my experience, it's almost always better to explicitly encode queries (or build them with a query builder) than to build them manually at run time.

On 14.08.21 15:43, Daniel da Silva wrote:
I know my resultset will only have one row. But I don't know about columns. e.g.

func GetData(db *sql.DB, ent Entity, tableName, uniqueField string, value 
interface{}) error {
    query := fmt.Sprintf("SELECT * FROM %s WHERE %s = $1", tableName, 
uniqueField)

    row := db.QueryRow(query, value)

    columns := []string{} // columns, err := row.Columns()

    if err := row.Scan(ent.Pointers(columns)...); err != nil {
        return err
    }

    return nil
}


On Saturday, August 14, 2021 at 5:08:12 AM UTC-3 Tamás Gulácsi wrote:

    It could, but what for? sql.Row is already a convenience wrapper
    for I-know-I-want-only-one-row,
    so you know what kind of data are you retrieving, but you know
    it's only one row?

    daniel...@omie.com.br a következőt írta (2021. augusztus 13.,
    péntek, 19:04:24 UTC+2):

        Yes i ever use Query() instead of QueryRow().
        My doubt is more about the API design.
        If exists *sql.Row with Scan() method why not implements
        Columns() ?

        On Thursday, August 5, 2021 at 3:43:44 PM UTC-3
        eli...@gmail.com wrote:

            In what case do you find the need to call Columns() on a
            Row? Generally if you have access to a Row, you also have
            access to a Rows, which does have a Columns() method.

            Eli

            On Thu, Aug 5, 2021 at 10:34 AM Daniel da Silva
            <dop...@gmail.com> wrote:

                Hello guys!

                Why doesn't sql.Row implement the Columns() method?

                Thanks!

-- 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...@googlegroups.com.
                To view this discussion on the web visit
                
https://groups.google.com/d/msgid/golang-nuts/1c521514-d267-456c-a66a-e56544965fd3n%40googlegroups.com
                
<https://groups.google.com/d/msgid/golang-nuts/1c521514-d267-456c-a66a-e56544965fd3n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
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+unsubscr...@googlegroups.com <mailto:golang-nuts+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/aa0085f1-7057-40fb-9539-3c133331e152n%40googlegroups.com <https://groups.google.com/d/msgid/golang-nuts/aa0085f1-7057-40fb-9539-3c133331e152n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
.
  Gregor Best
  b...@pferdewetten.de

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b067d62d-b412-e34f-6b8a-6e727b44d007%40pferdewetten.de.

Reply via email to