在Go语言中,接口是方法签名的集合,它也是一种类型,意味着您可以创建接口类型的变量。众所周知,Go语言不支持继承,但是Go接口完全支持嵌套。在嵌套过程中,一个接口可以嵌套其他接口,或者一个接口可以在其中嵌套其他接口的方法签名,两者的结果与示例1和2中所示的相同。您可以在单个接口中嵌套任意数量的接口。而且,如果对接口的方法进行了任何更改,则在将一个接口嵌套其他接口时,该接口也将反映在嵌套式接口中,如示例3所示。
语法:
type interface_name1 interface {
    Method1()
}
type interface_name2 interface {
    Method2()
}
type finalinterface_name interface {
    interface_name1
    interface_name2
}
或
type interface_name1 interface {
    Method1()
}
type interface_name2 interface {
    Method2()
}
type finalinterface_name interface {
    Method1()
    Method2()
}接口嵌套示例1:
package main
import "fmt"
// 接口 1
type AuthorDetails interface {
    details()
}
// 接口 2
type AuthorArticles interface {
    articles()
}
// 接口 3
//接口3嵌套接口1和接口2
type FinalDetails interface {
    AuthorDetails
    AuthorArticles
}
// 结构体
type author struct {
    a_name    string
    branch    string
    college   string
    year      int
    salary    int
    particles int
    tarticles int
}
// 实现接口1的方法
func (a author) details() {
    fmt.Printf("作者: %s", a.a_name)
    fmt.Printf("\n部门: %s 通过日期: %d", a.branch, a.year)
    fmt.Printf("\n大学名称: %s", a.college)
    fmt.Printf("\n薪水: %d", a.salary)
    fmt.Printf("\n发表文章数: %d", a.particles)
}
// 实现接口2的方法
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\n待定文章数: %d", pendingarticles)
}
func main() {
    // 结构体赋值
    values := author{
        a_name:    "Mickey",
        branch:    "Computer science",
        college:   "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }
    // 使用FinalDetails接口访问接口1,2的方法
    var f FinalDetails = values
    f.details()
    f.articles()
}输出:
作者: Mickey 部门: Computer science 通过日期: 2012 大学名称: XYZ 薪水: 50000 发表文章数: 209 待定文章数: 100
用法说明:如上例所示,我们有三个接口。接口1和2是简单接口,接口3是嵌套式接口,其中同时包含1和2接口。因此,如果接口1和接口2发生了任何变化,接口3都会反映出来。接口3可以访问接口1和2中存在的所有方法。
接口方法嵌套:
package main
import "fmt"
// 接口 1
type AuthorDetails interface {
    details()
}
// 接口 2
type AuthorArticles interface {
    articles()
}
// 接口 3
//接口3 嵌入了接口1和接口的方法
type FinalDetails interface {
    details()
    articles()
}
// 结构体
type author struct {
    a_name    string
    branch    string
    college   string
    year      int
    salary    int
    particles int
    tarticles int
}
// 实现接口1的方法
func (a author) details() {
    fmt.Printf("作者: %s", a.a_name)
    fmt.Printf("\n部门: %s 通过日期: %d", a.branch, a.year)
    fmt.Printf("\n大学名称: %s", a.college)
    fmt.Printf("\n薪水: %d", a.salary)
    fmt.Printf("\n发表文章数: %d", a.particles)
}
// 实现接口2的方法
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\n待定文章数: %d", pendingarticles)
}
func main() {
    // 结构体赋值
    values := author{
        a_name:    "Mickey",
        branch:    "Computer science",
        college:   "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }输出:
作者: Mickey 部门: Computer science 通过日期: 2012 大学名称: XYZ 薪水: 50000 发表文章数: 209 待定文章数: 100
用法说明:如上例所示,我们有三个接口。接口1和2是简单接口,接口3是嵌套式接口,在其中包含1和2接口方法签名。因此,如果接口1和接口2的方法发生任何更改,它将反映在接口3中。接口3可以访问接口1和2中存在的所有方法。
接口嵌套并同时拥有自己的方法的接口示例3:
package main
import "fmt"
// 接口 1
type AuthorDetails interface {
    details()
}
// 接口 2
type AuthorArticles interface {
    articles()
    picked()
}
// 接口 3
//接口3嵌套了接口1和接口2,同时加入了自己的方法
type FinalDetails interface {
    details()
    AuthorArticles
    cdeatils()
}
// author 结构体
type author struct {
    a_name    string
    branch    string
    college   string
    year      int
    salary    int
    particles int
    tarticles int
    cid       int
    post      string
    pick      int
}
// 实现接口1的方法
func (a author) details() {
    fmt.Printf("作者: %s", a.a_name)
    fmt.Printf("\n部门: %s 通过日期: %d", a.branch, a.year)
    fmt.Printf("\n大学名称: %s", a.college)
    fmt.Printf("\n薪水: %d", a.salary)
    fmt.Printf("\n发表文章数: %d", a.particles)
}
// 实现接口2的方法
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\n待定文章数: %d", pendingarticles)
}
func (a author) picked() {
    fmt.Printf("\n所选文章的总数: %d", a.pick)
}
// 实现嵌入了接口的方法
func (a author) cdeatils() {
    fmt.Printf("\n作者Id: %d", a.cid)
    fmt.Printf("\n提交: %s", a.post)
}
func main() {
    //结构体赋值
    values := author{
        a_name:    "Mickey",
        branch:    "Computer science",
        college:   "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
        cid:       3087,
        post:      "Technical content writer",
        pick:      58,
    }
    // 使用 FinalDetails 接口访问接口1,2的方法
    var f FinalDetails = values
    f.details()
    f.articles()
    f.picked()
    f.cdeatils()
}输出:
作者: Mickey 部门: Computer science 通过日期: 2012 大学名称: XYZ 薪水: 50000 发表文章数: 209 待定文章数: 100 所选文章的总数: 58 作者Id: 3087 提交: Technical content writer
用法说明:如上例所示,我们有三个接口。接口1和2是简单接口,而接口3是嵌套式接口,其中包含接口1的方法签名,接口2和它自己的方法。因此,如果接口1的方法和接口2发生任何更改,它将反映在接口3中。接口3可以访问接口1中的所有方法,包括接口1、2和它自己的方法。