Goang Receiver & Interface

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT") Type Pointerstruct{xstring}func ( This*Pointer) Printx () {fmt. Printf ("x:%s\n", This. X)} Func ( This*pointer) SetX (strstring) {     This. x =str}type Valuestruct{xstring}func ( ThisValue) Printx () {fmt. Printf ("x:%s\n", This. X)} Func ( ThisValue) SetX (strstring) {     This. x =str}func Main () {p1:= pointer{x:"Pointer"} p1. Printx () P1. SetX ("Pointer1") P1. Printx () P2:= &pointer{} p2. Printx () P2. SetX ("Pointer2") P2. Printx () P3:= value{x:"Value"} p3. Printx () P3. SetX ("Value1") P3. Printx () P4:= &value{x:"value"} p4. Printx () P4. SetX ("Value2") P4. Printx ()}

Operation Result:

X:pointerx:pointer1x:x:pointer2x:valuex:valuex:valuex:value

Description: 1 Either the t* as receiver or the T type as the recipient, the function can be called with an instance, or a pointer to an instance, but the t* as the receiver will change the contents of recipient, while the T type changes the copy, and the original object does not change.

Type typestruct{}type PTypestruct{}type InterInterface{post ()}//recipient non-pointerfunc (t Type) post () {}//the recipient is a pointerFunc (T *PType) post () {}func test () {varit Inter//var it *inter//interface cannot be defined as a pointerPty: = &type{} It= Ty//Assign the variable to the interface, OKIt.post ()//interface Call method, OK, receiver is Tty:=type{} It= Pty//Assign the pointer variable to the interface, OKIt.post ()//interface Call method, OK, receiver is TPty2:= &ptype{} It= Pty2//Assign the pointer variable to the interface, OKIt.post ()//interface Call method, OK, receiver is *t//ty2: = ptype{}//it = ty2//assigning a variable to an interface, error//it.post ()//interface Call method, error, receiver must be T, not t}

Detail code: Http://play.golang.org/p/KG8-Qb7gqM

Why does the compiler not consider our value to be the type that implements the interface? The invocation rules of an interface are based on how the recipients and interfaces of these methods are called. The following are the rules defined in the language specification, which are used to illustrate whether we have a type of value or pointer that implements the interface:

    • *Tthe set of callable methods for a type contains *T T all the set of methods that the recipient is or

This rule says that if the interface variable that we use to invoke a particular interface method is a pointer type, then the recipient of the method can be a value type or a pointer type. Obviously our example does not conform to the rule, because SendNotification the interface variable that we pass into the function is a value type.

    • Tthe set of callable methods for a type contains T all the methods that are accepted by the recipient

This rule says that if the interface variable that we use to invoke a particular interface method is a value type, then the recipient of the method must also be the value type that the method can be called. Obviously our example also does not conform to this rule, because Notify the recipient of our method is a pointer type.

There are only two rules in the language specification, and I have drawn the rules that conform to our example through these two rules:

    • Tthe callable method set of the type does not contain *T the method that the recipient is

Interface is also a reference type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.