Can we extend the interface defined in database/sql package?

Hi

Suppose in database/sql/driver/driver.go file, The interface Rows will define The functions columns which returns a string array. I want to return one more parameter to it.

Can we extend it and add a new function or override the existing function?

Thanks.

No, you can’t.

Any other piece of code that relies on the current interface would brake then.

So that means we can only implement the functions that are defined in the driver file.

Thanks.

You can implement functions definied in a different interface. Or even functions that are not part of an interface at all.

Is there any way to define a new function under that interface.

Which interface?

In Rows interface (database/sql/driver/driver.go)

As @NobbZ said above: No.

Ok Thanks.

Just to nuance this a bit… sounds like you are writing your own driver, so you have control over how it works. Then two things:
a) you can document access to your driver that does what you want. For example, the Postgres driver’s Listener feature [here].
b) you can offer the functionality by adding it to your interface and making your driver implement both interfaces. In this example, the standard would be like “x” and yours would be like “y”

Hope this gives you some ideas…

1 Like

Here’s an example of “extending” the interface definition - https://play.golang.org/p/j2ldYp5kplM

Thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.