Is there a way to make an interface also include the methods defined by another interface in Go?
For example:
type BasicDatabase interface {
CreateTable(string) error
DeleteTable(string) error
}
type SpecificDatabase interface {
CreateUserRecord(User) error
}
I would like a way to specify that the SpecificDatabase
interface contains the BasicDatabase
interface. Similar to the way Go lets you do composition of structs.
This way my methods can take a type that implements SpecificDatabase
, but still call CreateTable()
on it.
An interface type is defined as a set of method signatures. A value of interface type can hold any value that implements those methods.
To implement an interface in Go, you need to implement all the methods declared in the interface. Go Interfaces are implemented implicitly. Unlike Java, you don't need to explicitly specify using the implements keyword.
Composition is a method employed to write re-usable segments of code. It is achieved when objects are made up of other smaller objects with particular behaviors, in other words, Larger objects with a wider functionality are embedded with smaller objects with specific behaviors.
This can be done the same way as when composing structs.
type BasicDatabase interface {
CreateTable(string) error
DeleteTable(string) error
}
type SpecificDatabase interface {
BasicDatabase
CreateUserRecord(User) error
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With