This is a creation in
Article, where the information may have evolved or changed.
Today I saw the "Go language method, interface and embed type" so I would like to compare the previous knowledge of C + + to further understand Golang interface, pointer, parameter transfer.
Interface
According to the "Go language method, interface and embedded type" description can be seen, the interface to call the structure of the method needs to be different for the recipient to distinguish, that is:
- When the recipient is a pointer
*T , the interface instance must be a pointer
- When the recipient is a value
T , the interface strength can be either a pointer or a value
- The definition and type conversion of an interface is associated with the definition of the receiver
The example in the article is presented by the interface as a function parameter, where I use the variable directly to experiment:
PackageMainImport "FMT"typeTypestruct{Namestring}typePTypestruct{Namestring}typeInter IInterface {post ()}//Recipient non-pointerfunc(t Type) post () {FMT. Println ("POST")}//Receiver is a pointerfunc(t *ptype) post () {FMT. Println ("POST")}funcMain () {varIt Inter//var It *inter//interface cannot be defined as a pointerPty: =New(Type) Ty: = {"Type"} it = Ty//Assign the variable to the interface, OKIt.post ()//Interface call method, OKit = Pty//Assign the pointer variable to the interface, OKIt.post ()//Interface call method, OKPty2: =New(PType) Ty2: = {"PType"} it = Ty2//Assign the variable to the interface, errorIt.post ()//Interface call method, errorit = Pty2//Assign the pointer variable to the interface, OKIt.post ()//Interface call method, OK}
The situation that you see and the interface used in the article are exactly the same as the parameters.
According to the specified interface can not be defined as a pointer, I have tried to define the interface as a pointer, and to prompt:
Cannot use User (type user) as type *interface in assignment:
*interface is pointer to Interface, not Interface
Result of the failure. Personal experience with C + + This understanding interface, 接口本身是一种引用,而声明指向引用的指针是非法的 C + +, the following compilation error appears:
Error:cannot declare pointer to ' int& '
Variable and pointer direct call success should be a hidden transformation of the Golang internal type
Further exploration, the conversion of pointers to pointers via pointer assignment is successful pty = t.(*Type) , but the following error occurs when converting an interface that assigns values through pointers to ordinary value types:
Panic:interface Conversion:main. Inter is *main. Type, not main. Type
Similarly, an interface that is assigned a value directly to a pointer can pty = t.(Type) also go wrong, which may prove interface to be a reference inference, but also indicates that he is not a simple pointer.
This reminds me of the fact that virtual functions in C + + are polymorphic, and if you want to use polymorphic attributes, the instantiated object must be a pointer or reference.
Function
According to the experimental parameters are basically the same as the C + +, after all, go and c just "half-mother" only (:-P). That is, all parameters are passed by value, and pointers and references are the same as the replicas point to, so modifying their contents causes the original data to change. So the Golang function needs to modify the data to pass the data through the pointer type.
As with function arguments, if the modification of the recipient is a value type of data, it is only a modification of his copy, without affecting his original data.
Thus it is understood that the receiver actually passes a parameter, which is passed in the same way as copying
OK, first of all, according to their own understanding in the further additions and modifications, if there are errors, please also point out.
CSDN Blog Changed markdown Editor Writing article mood is really much better!!!!
2015-03-13
Cnsworder