Interface inheritance looks like the following in C#:
interface IA{
void MethodX();
}
interface IB : IA{
void MethodY();
}
How can I reuse interface definition in go?
Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces.
One interface can only extend other interface but cannot implement the other interface as the interface can only have method declarations and not the method body itself. When we say implements, that basically means to provide the method definition for the abstract methods.
C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.
Since Golang does not support classes, so inheritance takes place through struct embedding. We cannot directly extend structs but rather use a concept called composition where the struct is used to form other objects. So, you can say there is No Inheritance Concept in Golang.
You can embed other interfaces inside an interface, which gives you basicaly the same benefits:
A Good Example is the ReadWriteCloser
in the io package:
http://golang.org/pkg/io/#ReadWriteCloser
It embeds a Reader
, a Writer
and a Closer
interface.
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