English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang basic tutorial

Golang control statements

Golang function & method

Golang structure

Golang slice & array

Golang string (String)

Golang pointer

Golang interface

Golang concurrency

Golang exception (Error)

Golang other items

Go Language Interface Nesting

In Go language, an interface is a collection of method signatures, and it is also a type, which means you can create variables of interface type. As everyone knows, Go language does not support inheritance, but Go interfaces fully support nesting. In the nesting process, an interface can nest other interfaces, or an interface can nest the method signatures of other interfaces, and the results are the same as the example1and2The same as shown. You can nest any number of interfaces in a single interface. And if any changes are made to the methods of the interface, the interface will also reflect in the nested interface when nesting one interface in another, as shown in the example.3As shown.

Syntax:

type interface_name1 interface {
    Method1()
}
type interface_name2 interface {
    Method2()
}
type finalinterface_name interface {
    interface_name1
    interface_name2
}
Or
type interface_name1 interface {
    Method1()
}
type interface_name2 interface {
    Method2()
}
type finalinterface_name interface {
    Method1()
    Method2()
}

Interface nesting example1:

package main
import "fmt"
// Interfaces 1
type AuthorDetails interface {
    details()
}
// Interfaces 2
type AuthorArticles interface {
    articles()
}
// Interfaces 3
//Interfaces3Nested interfaces1And interface2
type FinalDetails interface {
    AuthorDetails
    AuthorArticles
}
// Structures
type author struct {
    a_name string
    branch string
    college string
    year int
    salary int
    particles int
    tarticles int
}
// Implement the interface1method
func (a author) details() {
    fmt.Printf("Author: %s", a.a_name)
    fmt.Printf("\nDepartment: %s Through Date: %d", a.branch, a.year)
    fmt.Printf("\nUniversity Name: %s", a.college)
    fmt.Printf("\nSalary: %d", a.salary)
    fmt.Printf("\nNumber of published articles: %d", a.particles)
}
// Implement the interface2method
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\nNumber of pending articles: %d", pendingarticles)
}
func main() {
    // struct assignment
    values := author{
        a_name: "Mickey",
        branch: "Computer science",
        college: "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }
    // Accessing the interface using FinalDetails interface1,2method
    var f FinalDetails = values
    f.details()
    f.articles()
}

Output:

Author: Mickey
Department: Computer science Through date: 2012
University name: XYZ
Salary: 50000
Number of published articles: 209
Number of pending articles: 100

Usage instructions:As shown in the example above, we have three interfaces. Interface1and2Is a simple interface, the interface3Is a nested interface that also contains1and2Interface. Therefore, if the interface1And interface2Any changes, the interface3Reflects. The interface3can access the interface1and2Contains all methods.

Interface method nesting:

package main
import "fmt"
// Interfaces 1
type AuthorDetails interface {
    details()
}
// Interfaces 2
type AuthorArticles interface {
    articles()
}
// Interfaces 3
//Interfaces3 Nested interfaces1And interface methods
type FinalDetails interface {
    details()
    articles()
}
// Structures
type author struct {
    a_name string
    branch string
    college string
    year int
    salary int
    particles int
    tarticles int
}
// Implement the interface1method
func (a author) details() {
    fmt.Printf("Author: %s", a.a_name)
    fmt.Printf("\nDepartment: %s Through Date: %d", a.branch, a.year)
    fmt.Printf("\nUniversity Name: %s", a.college)
    fmt.Printf("\nSalary: %d", a.salary)
    fmt.Printf("\nNumber of published articles: %d", a.particles)
}
// Implement the interface2method
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\nNumber of pending articles: %d", pendingarticles)
}
func main() {
    // struct assignment
    values := author{
        a_name: "Mickey",
        branch: "Computer science",
        college: "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }

Output:

Author: Mickey
Department: Computer science Through date: 2012
University name: XYZ
Salary: 50000
Number of published articles: 209
Number of pending articles: 100

Usage instructions:As shown in the example above, we have three interfaces. Interface1and2Is a simple interface, the interface3Is a nested interface, which contains1and2Interface method signature. Therefore, if the interface1And interface2Any method in the interface changes, it will reflect in the interface3. Interface3can access the interface1and2Contains all methods.

An example of an interface that nests interfaces and has its own methods3:

package main
import "fmt"
// Interfaces 1
type AuthorDetails interface {
    details()
}
// Interfaces 2
type AuthorArticles interface {
    articles()
    picked()
}
// Interfaces 3
//Interfaces3Nested interfaces1And interface2Joining its own methods
type FinalDetails interface {
    details()
    AuthorArticles
    cdeatils()
}
// author struct
type author struct {
    a_name string
    branch string
    college string
    year int
    salary int
    particles int
    tarticles int
    cid int
    post string
    pick int
}
// Implement the interface1method
func (a author) details() {
    fmt.Printf("Author: %s", a.a_name)
    fmt.Printf("\nDepartment: %s Through Date: %d", a.branch, a.year)
    fmt.Printf("\nUniversity Name: %s", a.college)
    fmt.Printf("\nSalary: %d", a.salary)
    fmt.Printf("\nNumber of published articles: %d", a.particles)
}
// Implement the interface2method
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\nNumber of pending articles: %d", pendingarticles)
}
func (a author) picked() {
    fmt.Printf("\nTotal number of selected articles: %d", a.pick)
}
// Implement embedded interface methods
func (a author) cdeatils() {
    fmt.Printf("\nAuthorId: %d", a.cid)
    fmt.Printf("\nSubmit: %s", a.post)
}
func main() {
    //struct assignment
    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,
    }
    // Use the FinalDetails interface to access the interface1,2method
    var f FinalDetails = values
    f.details()
    f.articles()
    f.picked()
    f.cdeatils()
}

Output:

Author: Mickey
Department: Computer science Through date: 2012
University name: XYZ
Salary: 50000
Number of published articles: 209
Number of pending articles: 100
Total number of selected articles: 58
Author ID: 3087
Submit: Technical content writer

Usage instructions:As shown in the example above, we have three interfaces. Interface1and2is a simple interface, while the interface3is a nested interface, which contains interfaces1method signature, the interface2and its own methods. Therefore, if the interface1methods and interfaces2Any changes will be reflected in the interface3. Interface3can access the interface1All methods, including interfaces1、2And its own method.