[Journey with golang] 4. Interface

golang interface with my existing knowledge is not the same, this article is to write for a while.


Interface is a program of the Statute, it is a set of methods of collection of signatures. Golang non-invasive interface design, i.e., a specific type of syntax on the implementation of the interface do not need to explicitly stated, as long as the particular type of methodology is a superset of the set of interface methods, it represents the type implements the interface the compiler at compile time check method set. The interface is no specific logic implementation, can not define the field.

Only interface variable value and type of concept, so the interface type variable is still known as interface variables stored in the internal interface specific type of variable is called "instance" of the interface points. Interface only statement did not realize, so the definition of a new interface, usually turned into a new interface to the statement, both general, the same meaning.

Literals most commonly used type of the interface is empty interface  interface {}  , since the methods set is empty empty interface, so that any type are realized empty interface, any type of example can be assigned or transferred to the empty interface, not including named instance type. Note: Since the unnamed type method can not define their own, the methods set is empty, so the air is passed to the type of a variable in addition to the interface, can not be transferred to any other interfaces.

golang interface into the interface type and interface named literal type, interface declaration to use  interface  keyword. Interface type literal statement syntax is as follows:

1 interface{
2     MethodSignature_1
3     MethodSignature_2
4 }

Name the interface type using the  type  keyword statement syntax is as follows:

1 type InterfaceName interface{
2     MethodSignature_1
3     MethodSignature_2
4 }

Use literal interface scenes rarely, usually only empty interface interface {} will declare a variable type of use.

Interface defines a set of braces may be declared method can also be embedded in another anonymous Interface Type field, you may be confusing the two. E.g:

 1 // Reader ...
 2 type Reader interface {
 3     Read(p []byte) (n int, err error)
 4 }
 5 
 6 // Writer ...
 7 type Writer interface {
 8     Write(p []byte) (n int, err error)
 9 }
10 
11 type readAndWrite interface {
12     Reader
13     Writer
14 }
15 
16 type readAndWrite2 interface {
17     Read(p []byte) (n int, err error)
18     Writer
19 }

golang function without "function declaration" is a special form of function, but there golang "method statement" essentially the type of method, instead of using the "method signature." Function signature in the strict sense is literal type of the function, the function signature does not include the name of the function, and the function declaration refers to the name of the function to bring the function signature. Similarly, the interface definition uses a method declaration, rather than the method signature. It can be said method declaration = method name + method signature. golang compiler is a strict check method name and method signature of the judge in making the interface to match.

Declare a new type of interface features:

  1. Naming interface generally er end
  2. The method defined internal interface declaration does not require a  func  guided
  3. In the interface definition, only the method declarations but there is no way to achieve

Only the interface is initialized when a specific type of sense. Interface as a glue layer, abstract and adaptation functions. No interface variables initialized, the default value is nil. Examples of specific binding interfaces process is referred to as the interface type initialization. Supports two direct interface variable initialization method: Example assignment interface and the interface variable copy interface variables.

If the method set specific examples of the type of an interface method is a superset of the set is said to achieve a specific type of interface, you can give the interface type of the variable, then the compiler will be the example of a specific type of direct assignment of static type checking. After the interface is initialized, call interface method is equivalent to a specific type of method invocation interface binding, which is the semantic interface calls.

Interface variable is assigned to the interface variables: initialized variable a type of interface to another interface to directly copy the variable b. This method requires b set is a subset of a set of methods. At this time, the compiler method golang set statically at compile time checking, the process is one way interface initialization, in which case specific examples of the binding interface variable b is a copy of a specific example of a binding interface variables.

Interface method calls and ordinary function calls are different. The method of the final address of the interface method invocation is determined at runtime, the specific type of the variable to assign the interface, the use of specific types of interface initialization method pointer variable, method call interface when the variable is actually calling the instance indirectly . Interface method call is not a direct call, there are certain operating overhead. Direct call interface uninitialized variable method will cause panic, for example:

 1 package main
 2 
 3 type printer interface {
 4     print()
 5 }
 6 
 7 type s struct{}
 8 
 9 func (ss s) print() {
10     println("print successfully")
11 }
12 
13 func main() {
14     var i printer // this is an interface example
15     // the following code will cause a panic
16     // i.print()
17     i = s{}   // an interface example must be inited, or it will cause panic
18     i.print() // call function print via interface example
19     var a s
20     a.print() // call function print via struct example
21 }

Interface type interface is divided into dynamic and static type interface. Specific examples of the type of interface is called dynamic binding type of interface. Examples of different types of interfaces can be bound, it is the dynamic type interface with different types of binding and examples thereof changes. If the interface is defined, its type has been determined, this type is called static type interface. Static type of the interface was determined at the time of its definition, it is the essential characteristics of the static type interface method signature set. If the method signature set two identical interfaces (the order may be different), then the two interfaces completely semantically equivalent, no cast between them can be assigned to each other. The reason is golang compiler can check whether the interface assignment, the comparative method is set between the two, rather than look at the specific type of interface type name.

Guess you like

Origin www.cnblogs.com/JHSeng/p/12179272.html