Why in the following example compiler says sql.Tx
does not implement driver.Tx
, seeing that sql.Tx
does indeed fulfil the interface:
import (
"database/sql"
"database/sql/driver"
)
func main() {
var myDB store = db{}
}
type store interface {
Store(tx driver.Tx)
}
type db struct {}
func (db) Store(tx *sql.Tx) {}
type Tx interface {
Commit() error
Rollback() error
}
./prog.go:9:6: cannot use db{} (type db) as type store in assignment:
db does not implement store (wrong type for Store method)
have Store(*sql.Tx)
want Store(driver.Tx)
Your implementation needs to match exactly, so Store() must accept a driver.TX type. Not only a *sql.Tx.
Because sql.Tx implements the driver.Tx interface it can be provided as the input.
import (
"database/sql"
"database/sql/driver"
)
func main() {
var myDB store = db{}
sqlTx := &sql.Tx{}
myDB.Store(sqlTx)
}
type store interface {
Store(tx driver.Tx)
}
type db struct{}
func (db) Store(tx driver.Tx) {}