This is a creation in Article, where the information may have evolved or changed.
PS: I have time to study on weekends, so I even remember two notes.
About interfaces in the go language: assigning values to interfaces
The interface of GO is very important according to the introduction of "Go Language programming".
On the implementation of the interface, there is a concept: in theGo language, a class only needs to implement the interface requirements of all functions, we say that this class implements the interface (emphasis)
For example, there is an interface (this does not want to use the original book example, but the description is really simple)
Type integer int//a new int-based integer
Type MyInterface Interface {//This is an interface this is a go language interface Ah, this interface is long this virtue AH
Less (b integer) integer
ADD (b Intteger)
}
The next step is to implement this interface, which is conceptually
Func (a integer) less (b integer) integer{//parameter type description: (a integer), which represents the method binding on the type Integer, equivalent to providing a class method to a class; less: Method name; b: Formal parameter name
Do something ...
}
Func (a *integer) Add (b Integer) {
Do a little pointers on what to do.
}
The focus is on the process of using
Func Main () {//very much like C's main function Yes, really like
var a Integer = 1
var b Integer = 2
var c myinterface = &a//As for why a reference to a value, not a value, is to maintain the implementation of the interface parameter type unification, as for the reason reference "Go ..." You know this book
C.add (b)//This step is crucial to my small white, almost forgot c This variable value, is not an integer type, it is a variable of the interface type
Above this is called C interface, dry one thing, is to use the Add this method, and with B this variable took place some ambiguous things, can Lenovo Cang teacher ...
C This interface finished the Lenovo teacher's thing, the beneficiary, is the variable A, because C uses a reference address
Soso
Fmt. Println (a)
This step you will get a value of 3, this implementation process, from the interface MyInterface
}
PS: Ms. Cang is also a goddess ...
PS: "Go language Programming" is my main reference books, so later this book referred to as "Go ..." for the simple philosophy of programming salute