We use function New(db DBTX) to get access to receiver methods, but we must pass to the function an object that implements interface DBTX. I noticed that we can just pass there database connection *sql.DB and it works even though *sql.DB doesn’t implement that interface. How does it work?
Ok, I think I understand. db package has these methods implemented, thus when I create my own interface with the same methods (same signature) it means db package also implements my interface. Is it right?
Yes that is correct.
If your struct has functions attached with the same signature (function name, arguments and return values) it will be automatically also a DBTX.
This is how inheritenace is working in go.
A nice example that is maybe often used is io.Writer (io package - io - Go Packages).
So if your class (struct) implements the function Write(p []byte) (n int, err error) it will be automatically an io.Writer and can be passed to every call that expects an io.Writer.