Go language, scanning embeded struct with sqlx.StructScan
<p>I have just started learning Go language.
I wrote following simple program.</p>

<p>Here i am trying to fill struct with all Books and related Authers. </p>

<p><code>Book</code> struct has embeded <code>Author</code> struct.</p>

<pre><code>package main
import (
    "fmt"
    "log"
    "time"
    "github.com/jmoiron/sqlx"
    _ "github.com/lib/pq"
)

type Book struct {
    ID      int
    Title   string
    Year    int
    Bauther  Auther `db:"auther"`
}

type Auther struct {
    ID      int
    Name    string
    Dob     time.Time
}

func main() {
    db, err := sqlx.Open("postgres", "host=localhost user=testuser dbname=testdb password=testuser")
    if err != nil {
       log.Fatal("DB Conn error: ", err)
    }

    if err = db.Ping(); err != nil {
        log.Fatal("DB Ping error: ", err)
    }
    defer db.Close()

    rows, err := db.Queryx("Select b.*, a.name from books b left outer join authers a on a.ID=b.auther;")
    if err != nil {
        log.Fatal("DB Query error: ", err)
    }
    defer rows.Close()

    var books []*Book
    for rows.Next() {
        var b = &amp;Book{}
        err := rows.StructScan(b)
        if err != nil {
            log.Fatal("Scan error: ", err)
        }
        books = append(books, b)
    }

    // print all books
    for _, b := range books {
        fmt.Printf("%v", b)
    }
}
</code></pre>

<p>But when i run it, it is giving following error</p>

<pre><code>[samtech@sam sqlxapp]$ go run main.go
2016/02/11 18:45:46 Scan error: missing destination name name
exit status 1
</code></pre>

<p>What i am doing wrong?</p>

<p>I have also tried changing field tag in <code>Book</code> struct to</p>

<pre><code>Bauther  Auther `db:"auther,prefix=auth."`
</code></pre>

<p>and change query to</p>

<pre><code>rows, err := db.Queryx("Select b.*, auth.name from books b left outer join authers auth on auth.ID=b.auther;")
</code></pre>

<p>But it doesn't make any change.</p>

<p><strong>EDIT</strong></p>

<p>After few try and errors, finally i make it working.</p>

<p>I have to slightly change the Models that i have created. I changed Book struct from</p>

<pre><code>type Book struct {
    ID      int
    Title   string
    Year    int
    Bauther Auther
}
</code></pre>

<p>to </p>

<pre><code>type Book struct {
    ID          int                  // Key
    Title       string
    Year        int
    AutherID    int `db:"auther"`   // FKey
    Auther 
}
</code></pre>

<p>Now, it is working fine. The mistake i was doing was, i added <code>Bauther</code> field as <code>Auther</code>. Sqlx could not understand it. But when i added <code>Auther</code> as anonymous embedded struct, the problem resolved.</p>

<p>But it introduces another problem :)</p>

<p>As ID field exist in <code>Book</code> as well as in <code>Auther</code> both the structs. Now ScanStruct is filling <code>Book.ID</code> with 0 in all rows.</p>

<p>Is there anything that i can do to avoid it?</p>