Home  >  Article  >  Backend Development  >  Can golang interfaces be nested?

Can golang interfaces be nested?

angryTom
angryTomOriginal
2020-03-12 12:41:042623browse

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 nesting between 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 the golang tutorial column 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn