golang generic polymorphic (methods, interfaces and AnonymousField)

method

func (s *Struct1)funcName(a,b int) (stringint) {
    ...
}
  • Method one more function than the recipient

    • The method of acting on a structure or create their own type
      • Similar in c ++ member function
  • Method statement and the user can not a file, but in a package

  • Two types of receivers: the pointer value receiver and the receiver

    • Use value type recipient defined method, when invoked, in fact, used copy of the value , so the value of any operation, will not affect the original variables
    • If you want to make changes to the method of the recipient, then it should be used as a pointer to the recipient, using a pointer to a copy of the
    • It is analogous to the first parameter recevier method
    • When in use, value or pointer can be called directly, according to the statement automatically take the address or dereference
  • String Methods

    • The print function will automatically call its String method when printing custom data types
    • func (t T) String() string

interface

Here Insert Picture Description

  • If a certain type implements an interface all methods, all places that use this interface, can support the value of this type of
  • Do not use a pointer to an interface type, because the interface itself is already a pointer
  • When you call the interface called after the jump dynamic computing will lead to failure of CPU cache and branch prediction fails
  • Interface variable initialization makes sense, default is nil
  • Air interface may be implemented in any type of
    • type empty interface{}
    • Therefore, the use of air interfaces can be implemented generic
  • The compiler could check the interface is assigned a set of methods both comparison rather than specific interface type name,
    • If the method A set is a superset of the interface B, the interface variable A can be assigned directly to B
  • Implementation of the underlying interface variables
    • Value of the interface is a data structure of a word length
    • The first word contains a pointer to the internal structure of the table, the method sets this internal list memory interface types, examples of the type of binding and the associated
    • The second word contains a pointer to a stored entity type value

Here Insert Picture Description
Here Insert Picture Description

  • Type assertion

    • grammar:i.(TypeName)
      • i must interface variable
    • If TypeName is the specific type, for the determination of the binding instance whether the type of the interface that the particular type
    • If TypeName is the type of excuse, it will determine whether binding instance of the interface simultaneously implements this interface type
    type Element interface{
    	io.Reader
    }
    // element必须为借口类型
    var element Element = 1
    
    if value,ok := element.(int); ok {
        ...
    } 
    
    if iface, ok := element.(io.Reader); ok {
    	...
    }
    
  • The type of query

    • It will be very useful in processing, unknown data type from the outside, such as parsing data such as JSON or XML-encoded
    • grammar:switch v := i.(type){}
      • i must interface variable
      • If i is not bound in any instance, then v is nil
    • Later case either with the particular type, with an interface type may be
    • You can not use fallthrough
    switch v := i.(type) {
    case nil:
    	...
    case string:
        ...
    }

	switch v:= i.(type) {
	case io.Reader, io.Writer:
		...
	}
  • The method set
    • The method set value type variable value contains only the recipient declaration method
    • Methods set contains a pointer type variable and pointer values ​​of the recipient declaration
// 指针声明的方法
func(u *User) notify() {
	...
}

type notifier interface {
	notify()
}

func sendNotify(n notifier) {
	n.notify
}

var u User
// 下面的函数编译不通过
// 因为User类型只实现了指针方法集,不能传入值
sendNotify(n)
// 下面的函数通过
sendNotify(&n)
// 上面这样规定的原因是编译器不是总能自动获得一个值得地址,例如这个值是一个常数25
、、

Anonymous field

  • Only the structure type name without a field variable name
  • Whereupon the internal structure inherits the fields and methods of inline anonymous field
    • This can simulate inheritance
    • Types of methods can be called directly inside: outer.innerfunc()
    • If the internal type implements an interface, the external type is also automatically
  • If there are ways of external and internal types of the same name type, it will cover the internal method
    • At this time if you are calling internal method, it is necessary to explicitly accessed by accessing the internal type
    • outer.inner.func()
  • From an existing non-interface when creating a new type of type, is not inherited by the original method
    • If you need to use the original type of method, the original type can be embedded into your new struct defined in the form of an anonymous field
// myMutex没有Lock()方法
type myMutex sync.Mutex
// myMutex有Lock()方法
type myLocker struct {
    sync.Mutex
}
Published 161 original articles · won praise 19 · views 50000 +

Guess you like

Origin blog.csdn.net/winter_wu_1998/article/details/102734223