learned a little about Go, and found that the design of the Go syntax is very simple and easy to understand. It is the "Less is more" that is said by Rob Pike, the father of the Go language - road to Jane.

underneath the specific grammatical features of my own experience. The overview of


is different from the object-oriented design (OOP) language, such as C++ and Java, which is usually based on type hierarchy and inheritance. The core idea of Go is combination (composition). Go further decoupling of the object and operation, to achieve a real duck type (Duck typing): an object that can be as if quacking ducks, not like C++ or Java that need to ensure that the type system: a first object is a duck, then quack.

 type Duck interface {   Quack() }  type Animal struct {   name string }  func (animal Animal) Quack() {   fmt.Println(animal.name, ": Quack! Quack! Like a duck!") }  func main() {   unknownAnimal := Animal{name: "Unknown"}    var equivalent Duck   equivalent = unknownAnimal   equivalent.Quack() }


Unknown : Quack! Quack! Like a duck!

下面用 Java 语言来实现:

 interface Duck {   void Quack(); }  class SomeAnimal implements Duck {   String name;    public SomeAnimal(String name) {     this.name = name;   }    public void Quack() {     System.out.println(name + ": Quack! Quack! I am a duck!");   } }  public class Test {   pub LIC static void main (String []args) {SomeAnimal unknownAnimal = new SomeAnimal ("Unknown"); Duck equivalent = unknownAnimal; equivalent.Quack (

);}} comparison can be seen: Go object and its operation (method or function) decoupling more thoroughly. Go does not need an object to guarantee the realization of an interface (is a) through the type system. It only needs this object to implement an interface method (like a), and the type declaration and method declaration or implementation are loosely coupled. If a little change method implementations:

 func (animal) Quack (Animal) {fmt.Println (animal.name, "Quack! Quack! Like a duck!"): 



func Quack (animal Animal) {fmt.Println ("animal.name: Quack! Quack! Like a duck!"

is not the same)} and the general method?

in a text message in Cocoa I have analyzed the process of message call Objective C:

 Bird * aBird = [[Bird alloc] init]; [aBird fly]; 

in the call to fly, the compiler by inserting some code, convert to the other method of concrete realization of the call to IMP, this is IMP look through the list method in the Bird class structure in the name of specific methods fly SEL find the corresponding implementation, the compiler will convert the message message call function call to objc_msgSend:

 objc_msgSend (aBird, @selector 

(fly)); both the Signal/Slot mechanism of Objective message mechanism C or Qt, can be said to be trying to object itself (data) and the operation of the target (News) decoupling, but Go will this work Doing more thoroughly at the language level not only avoids multiple inheritance problems, but also reflects the most important thing in object oriented design: message passing between objects. The implementation of

interface by

is actually a structure containing two members. One of the members is a pointer to the specific data, and the other contains type information. The air interface and the interface method with slightly different, here are the air interface and method of interface is the use of the data structure:

 struct Eface void* data {Type* type;} Iface {Itab*; struct; tab; void* data;}; struct Itab {InterfaceType* inter; Type* type; Itab* link; int32 bad int32; unused; void (*fun[]) (void);}; struct Type {uintptr size; uint32 hash; uint8 _unused; uint8 align; uint8 fieldAlign; uint8 kind; Alg *alg; void *gc; String *string; UncommonType *x; Type *ptrto;}; 

look at Eface, it is the data structure of the underlying interface{} the use of. The data field contains a void* pointer, and a pointer to a type structure. Interface{} plays the same role as void* in the C language, and any object in Go can be represented as interface{}. The difference is that there are types of information in interface{}, so reflection can be achieved. The type of information structure

different types of data is not entirely consistent, Type is part of the public type information structure, which describe the type of size size, UncommonType is a pointer to an array of function pointers, collection of this type of concrete realization method of all.

has a KindOf function in the reflect package and returns a Type of interface{}, which is actually a Type domain in the Eface.

Iface is slightly different from Eface, and it is the data structure used on the bottom of the method with a method of interface. The data domain also points to the original data, and the Itab stores not only Type information, but also a method table fun[]. The method implemented in a specific type in Iface will be copied into the fun array of the Itab. There is a method table in the UncommonType of

Type, and all the methods implemented by a specific type will be collected in this table. Both the Method and MethodByName methods in the reflect package are implemented by querying this table. Each item in the table is a Method, the

 struct Method data structure: 

{String *name; String *pkgPath; Type *mtyp; Type *typ; void (*ifn) (void); void (*tfn) (void);};

Iface Itab InterfaceType also has a table this method, table method interface statement. Each of which is a IMethod data structure is as follows:

 struct IMethod *name String {String; *pkgPath; Type *type;}; Method 

structure compared with the above can be found here is the only statement did not achieve. The func domain of Itab in

Iface is also a method table, and each item in this table is a function pointer, that is, only the implementation is not declared. The detection of

type transformation is to see if the method table in Type contains all the methods in InterfaceType's method table, and the implementation part of Type method table is copied to the func table of Itab.


, a interface, when no initialization is initialized, the corresponding value is nil. That is to say,

 var V interface{}

at this time v is a nil. On the underlying storage, it is an empty pointer. The difference between



 var obj *T var V interface{} v = V, which is a value, which means that its value is empty, but it is not. 

following facts:
Go language in error type is actually Error (error) Abstract: the

 type interface error interface Error (string) {


has the following code:

 type Error struct {errCode uint8} func (E *Error) Error (string) e.errCode {switch {default:}} "return unknown error" func test_checkError (E *Error if) {var e = = nil {fmt.Println ("E is nil")} else {fmt.Println ("E is not nil var err error ERR)} err {if = e = = nil fmt.Println (" err is nil "else {fmt.Println}) (" err is not nil}

(test_checkError)} operation) output:

e is nil
err is

not nil

is the The entire content of the text, I hope to help everyone's learning, and also hope that a lot of people support the script home.

This paper fixed link:http://www.script-home.com/the-specific-use-of-go-interface.html | Script Home | +Copy Link

Article reprint please specify:The specific use of Go interface | Script Home

You may also be interested in these articles!