Saturday, June 29, 2024
HomeGolangSQLC DBTX interface - Getting Assist

SQLC DBTX interface – Getting Assist


I’ve began utilizing SQLC and I encountered a small drawback.
Auto-generated code appears to be like like this:

sort DBTX interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Consequence, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

func New(db DBTX) *Queries {
	return &Queries{db: db}
}

sort Queries struct {
	db DBTX
}

We use operate New(db DBTX) to get entry to receiver strategies, however we should cross to the operate an object that implements interface DBTX. I observed that we are able to simply cross there database connection *sql.DB and it really works although *sql.DB doesn’t implement that interface. How does it work?

Okay, I believe I perceive. db bundle has these strategies applied, thus once I create my very own interface with the identical strategies (similar signature) it means db bundle additionally implements my interface. Is it proper?



1 Like

Sure that’s appropriate.
In case your struct has capabilities connected with the identical signature (operate identify, arguments and return values) it is going to be routinely additionally a DBTX.
That is how inheritenace is working in go.

A pleasant instance that’s possibly usually used is io.Author (io bundle – io – Go Packages).
So in case your class (struct) implements the operate Write(p []byte) (n int, err error) it is going to be routinely an io.Author and may be handed to each name that expects an io.Author.

Matthias



1 Like

Whereas the remainder of your reply is spot on, I do need to be pedantic and tackle this assertion.

Go in-fact doesn’t help inheritance; as an alternative it helps composition by way of sort embedding. In actual fact, composition and kind embedding are orthogonal to interfaces.

What that is as an alternative is an instance of implicit interfaces.” #fwiw

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments