如何从另一个函数调用 dbmap.Insert(interface{})?

How can I call dbmap.Insert(interface{}) from another function?

我有一堆非常相似的结构(示例中的 A 和 B),我想在某些函数中处理它们的实例(示例中的 f()),然后将它们插入到我的数据库中。我想我可以以某种方式用空接口处理它,但似乎这不是解决方案,因为我收到错误:

i: &{{6 2019-04-03 15:11:37.822100431 +0200 CEST m=+0.001291882} 7} *main.A
2019/04/03 15:11:37 Insert i no table found for type: 
exit status 1

我尝试创建一些最小但可执行的示例:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/mattn/go-sqlite3"
    gorp "gopkg.in/gorp.v2"
    "log"
    "time"
)

type Meta struct {
    Id        int
    CreatedAt time.Time
}

type A struct {
    Meta
    Value int
}

type B struct {
    Meta
    Value string
}

var dbmap *gorp.DbMap

func f(i interface{}) {
    fmt.Printf("i: %v %T\n", i, i)

    err := dbmap.Insert(&i)
    checkErr(err, "Insert i")
}

func main() {
    Init()

    a := A{Meta: Meta{CreatedAt: time.Now()}, Value: 7}
    b := B{Meta: Meta{CreatedAt: time.Now()}, Value: "seven"}

    err := dbmap.Insert(&a)    // works
    checkErr(err, "Insert a")

    err = dbmap.Insert(&b)     // works
    checkErr(err, "Insert b")

    f(&a) // fails
}

func Init() {
    db, err := sql.Open("sqlite3", "/tmp/post_db.bin")
    checkErr(err, "sql.Open failed")
    dbmap = &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
    dbmap.AddTableWithName(A{}, "As").SetKeys(true, "Id")
    dbmap.AddTableWithName(B{}, "Bs").SetKeys(true, "Id")
    err = dbmap.CreateTablesIfNotExists()
    checkErr(err, "Couldn't create tables")
}

func checkErr(err error, msg string) {
    if err != nil {
        log.Fatalln(msg, err)
    }
}

正确的做法是什么?在 C++ 中,我只是使用模板 ;)

如果你像f(&a) 那样称呼你。您应该在 func f 内部调用 dbmap.Insert(i),因为您的值已经是一个指针。所以你的 func 看起来像

func f(i interface{}) {
    fmt.Printf("i: %v %T\n", i, i)

    err := dbmap.Insert(i)
    checkErr(err, "Insert i")
}