Home>Article>Backend Development> Can golang interfaces be nested?
Can golang interfaces be nested?
In Go language, not only structures can be nested,New interfaces can also be created by nestingbetween interfaces.
An interface can contain one or more other interfaces, which is equivalent to directly listing the methods of these embedded interfaces in the outer interface. As long as all methods of the interface are implemented, all methods of nested interfaces in this interface can be called.
For example, the interface File contains all methods of ReadWrite and Lock, and it also has an additional Close() method.
type ReadWrite interface { Read(b Buffer) bool Write(b Buffer) bool } type Lock interface { Lock() Unlock() } type File interface { ReadWrite Lock Close() }
For more golang knowledge, please pay attention to thegolang tutorialcolumn on the PHP Chinese website.
The above is the detailed content of Can golang interfaces be nested?. For more information, please follow other related articles on the PHP Chinese website!