This is the 15th day of my participation in Gwen Challenge
These reviews
Previous articles have focused on anonymous functions and closures in the Go language. The anonymous function of Go is a closure. Most languages that support closures treat functions as first-level objects, meaning they can be stored in variables and passed as arguments to other functions, and most importantly, they can be created and returned dynamically by functions.
The Go language provides another data type, the interface, which defines all common methods together, and any other type that implements those methods implements the interface. In Go, not only structures can be nested, but interfaces can also be nested to create new interfaces.
This article continues with the declaration and nesting of interfaces.
Interface declaration and nesting
An interface is a cooperative agreement between the caller and the implementer. Callers do not care about how the interface is implemented, but implementers expose their inherent functionality through the interface. Each interface consists of one or more methods, styled as follows:
type interfaceName interface {
method1(params)(return params)
method2(params)(return params)
method3(params)(return params)
...
}
Copy the code
To define an interface, use the type and interface keywords. When both the first letter of the interface name and the first letter of the method name are uppercase, the method is public and can be accessed outside of the package in which the interface resides. The concepts of packages will be described in a later section.
We can define a port for a tank that can be carted away or fired, as follows:
type Tank interface {
Walk() / / away
Fire() / / fire
}
Copy the code
In addition to tanks, we also have planes, which have relatively simple functions and can only fly, as shown below:
type Plane interface {
Fly() / / flight
}
Copy the code
If we have a new weapon that can be transported and fired like a Tank or flown like an airplane, we can use Golang’s interface nesting feature to nest the Tank and Plane interfaces to create a new interface, as shown in the following code:
type PlaneTank interface {
Tank
Plane
}
Copy the code
PlaneTank has nested Tank and Plane interfaces, so its implementer can function as both a Tank and an aircraft. Golang’s interface nesting feature makes it possible to create new interfaces by implementing interface inheritance features similar to those found in object-oriented programming.
summary
This article focuses on the definition and example use of interface declarations and nesting. In Go, the nested composition of interfaces makes it possible for an interface to contain one or more other interfaces, which is equivalent to listing the methods of those embedded interfaces directly in the outer interface. Methods of all nested interfaces in an interface can be called as long as all methods of the interface are implemented.
Similar to C, Golang provides structure types. As a compound type, a structure consists of multiple fields, each of which has its own type and value. Structures and fields can be understood as entities and their corresponding attributes. In the following articles, we will begin to introduce the definition and use of structure-related concepts.