package mysql
import (
"testing"
"github.com/ilaziness/gokit/config"
"github.com/stretchr/testify/assert"
)
var cfg = &config.DB{
DSN: "root:root@tcp(127.0.0.1:3306)/ent_test",
}
type User struct {
ID int `db:"id"`
Age int `db:"age"`
Name string `db:"name"`
Username string `db:"username"`
CreatedAt string `db:"created_at"`
}
func init() {
InitSqlx(cfg)
}
func TestInitSqlx(t *testing.T) {
u := User{}
err := sqlxDB.Get(&u, "SELECT * FROM users LIMIT 1")
assert.Equal(t, nil, err)
assert.Greater(t, u.ID, 0)
t.Log(u)
}
示例都是要先import _ "github.com/go-sql-driver/mysql"
测试下来不import也能正常查询出来。
找了下sqlx里面并没有import的操作,同样的我测试了下ent也是这样。
你的下面这个部分代码导致引入了
即
InitSqlx
方法所在的文件里面https://github.com/ilaziness/gokit/blob/557f819880d7b8b712995debbfd5a858bd26b308/storage/mysql/mysql.go#L12C3-L12C23