GO value method and the method of expression usage

The manual explain this feeling is not very clear in detail, through several examples of his summary of the usage of the next piece.

The method of expression: that simple point, in fact, is the way the object assigned to the variable.

There are two ways:

1) Method Found: implicitly calls, struct method for obtaining an object instance

2) Method expression: shows the call, target acquisition method struct type, struct need to pass an object instance as a parameter.

for example:

main Package 

Import ( 
    " FMT " 
) 

type Student struct { 
    ID    int 
    name String 
} 

FUNC (S * Student) SkillPointer () { 
    fmt.Printf ( " point type function:% P,% V \ n- " , S, S) 
} 

FUNC (Student S) SkillValue () { 
    fmt.Printf ( " value type function:% P,% V \ n- ' , & S, S) 
} 

FUNC main () { 
    S: = {Student . 1 , " Joe closer " } // structure instantiated
     //Conventional use 
    s.SkillPointer () 
    fmt.Println ( " ............................. \ n- " ) 

    // the expression formula 
    sfunc1: = (* Student) .SkillPointer // . Note that the structure used as a pointer type name of the method 
    sfunc1 (& S)                         // display the last transfer recipient * Student 

    sfunc2: = Student.SkillValue // Note that directly structure name of the method 
    sfunc2 (S)                     // display the recipient Student pass past 
    fmt.Println ( " .......................... ... \ the n- " ) 

    // method value 
    sFunc3: = s.SkillPointer // this method is the value, the function is called, no longer need to pass the recipient, the recipient is hidden
    sFunc3 ()                  // a bit like an anonymous function call, we declare the variable sFunc3 for the method name structure, then the variable () call. I am feeling a little superfluous hair? 

    sFunc4: = s.SkillValue // because there is no method to call pointer, copy the contents of the operation done here, where the difference in, for example will see later 
    sFunc4 () 
    fmt.Println ( " ........... .................. \ n- " ) 
}

Output:

Pointer type function: 0xc00007c060, & { . 1 Joe closer} 
............................. 

pointer type function: 0xc00007c060, & { . 1 Joe closer} 
value type function: 0xc00007c0e0, { . 1 Joe closer} 
............................. 

pointer type function: 0xc00007c060, & { 1 Joe closer} 
value type function: 0xc00007c140, { 1 Joe closer} 
.............................

Just said means that we should copy the way, what's the difference, let's take a look:

main Package 

Import ( 
    " FMT " 
) 

type Student struct { 
    ID    int 
    name String 
} 

FUNC (S * Student) SkillPointer () { 
    fmt.Printf ( " point type function:% P,% V \ n- " , S, S) 
} 

FUNC (Student S) SkillValue () { 
    fmt.Printf ( " value type function:% P,% V \ n- ' , & S, S) 
} 

FUNC main () { 
    U: = {Student . 1 , " Joe closer " } 
    mValue: = u.SkillValue// because it is not a pointer type, is not affected by changes below. 

    u.id, u.name = 2 , " Jiu Mozhi " 
    u.SkillValue () 
    mValue () // value unchanged, unaffected 
}

Output:

Value Type Function: 0xc0000044c0 , { 2 Jiu Mozhi} 
Value Type Function: 0xc000004520 , { . 1 Joe closer}

If you want all the updates, replaced  mValue: pointer to function type = u.SkillPointer.

Guess you like

Origin www.cnblogs.com/phpper/p/12370086.html