在我的
尽管我在两个文件中都导入了
internal/platform/database/database.go
文件中。
import (
"github.com/golang-migrate/migrate"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
func RunMigrations() error {
m, err := migrate.New(
"file://schema",
"postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable")
if err != nil {
return errors.Wrap(err, "error creating migrations object")
}
以下是从 cmd/my-api/main.go
中调用此函数的方式:
import (
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/myrepo/myproject/internal/platform/database"
)
// =========================================================================
// Executing migrations
if err := database.RunMigrations(); err != nil {
log.Fatal(err)
}
尽管我在两个文件中都导入了
postgres
驱动程序,_ "github.com/lib/pq"
,但运行程序会失败,如下所示:error creating migrations object: source driver: unknown driver file (forgotten import?)
exit status 1
为什么会这样呢?