This is a creation in Article, where the information may have evolved or changed.
Type $name struct{ property01 int property02 int }
the methods and interfaces inside the Golang are based on the type created by this type of struct here, which can actually be understood as:
Class $name {public int property01; public int property02; }
A type is a class.
So we're talking about a type of method that implements an interface.
A type is a collection of properties, an interface is a collection of methods
definition of the function: func $ FuncName () () {}
definition of the method: func () $funcName () () {}
Func ( member variable type ) funname ( local variable type , local variable type ) ( return value type ) {}
Member variables are defined by type .
The argument list for a function is a local variable that needs to be passed.
The type signature of the Golang method: 1. Specify which type to add the method to, 2. Specifies whether the variable calling this method is a value type or a pointer type, and the variable calling this method must be signed by Type here to determine whether the value type or pointer type, Golang can be converted automatically, but you must ensure that the variable can be correctly converted to the corresponding Pointer. For example, a variable of an interface type cannot be converted to a pointer to a struct.
Inherited:
When a typeBone of the fields(Anonymous Fields)type is another typeAthe time,sotypeAall of the fields that are owned are implicitly introduced to the currently definedtypeB. this implements the inheritance. a variable of type B can call All properties and methods of a. In other words , a inherits B.
when defining inheritance, a subclass typically contains a type that is a parent class. Anonymous Fields . An anonymous field is used to implement inheritance .
Package Mainimport ("FMT") type Animal struct {Name stringage int}func (ANI *animal) getage () int {return ANI. Age}type Dog struct {Animal//animal anonymous field}func main () {dog: = dog{animal{"Dog", 10}}fmt. Println (dog. Age) Fmt. Println (dog. Getage ())}
Override of Method
If a type B implements the method in type a as its property. Then the value of this type B calls the method of its own type B, rather than the method of the property type A.
The code is as follows:
Package Mainimport ("FMT") type Animal struct {Name stringage int}func (ANI *animal) getage () int {return ANI. Age}type dog struct {Animal//animal anonymous field}func (ANI Dog) getage () int {return ANI. Age + 1}func Main () {dog: = dog{animal{"Dog", 10}}fmt. Println (dog. Age) Fmt. Println (dog. Getage ())}
Interface
1 Interface
1) Define the interface type
Defines an interface in which an unassigned method can be implemented.
Type Animal Interface {getage () int}
1) Implementing the interface type
If a type implements all methods of an interface. This interface is implemented by this type.
Type Animal Interface {getage () Int}type Dog struct {Name stringage int}
Implement the getage () Square rule to implement the Animal interface
Func (Ani Dog) getage () int {return ANI. AGE}