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

Can golang interfaces be nested?

angryTom
Release: 2020-03-12 12:41:04
Original
2678 people have browsed it

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()
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template