Hang go the way of growth (1)

A first look at a piece of code

package main

import "fmt"

type X struct{}

func (x *X) test(){
    println("h1",x)
}
func main(){
    a := X{} 
    a.test()

    (&X{}).test()

    (X{}).test()
}

猜猜他的结果

Second, reveal the answer

package main

import "fmt"

type X struct{}

func (x *X) test(){
    println("h1",x)
}
func main(){
    a := X{} 
    a.test()   // 正确

    (&X{}).test()  // 正确 

    (X{}).test()  // 报错 cannot call pointer method on X literal
}

Third, why is this

  • Call the method after the pointer assignment statement
a :=x
a.test()  //正确
   指针方法可以调用的条件:
       receiver 必须是合法的指针(包括nil) 或者 能够获取实例的地址 
    a 是一个可以寻址的变量 ,所以可以调用test() 指针方法

    When the value is addressable, the language takes care of the common case of invoking a pointer method on a value by inserting the address operator automatically.
    翻译:
    当值是可被寻址的,go语言会处理通常的情况:
        在一个值上面调用它的指针方法,编译器会自动插入一个&取地址操作符
  • (& X {}). Test () correctly
   指针方法可以调用的条件:
       receiver 必须是合法的指针(包括nil) 或者 能够获取实例的地址 

    (&X{})  是一个合法的指针
  • (X {}). Test () error
    变量名 = 右值
    X{}  就是右值
    右值 是不可寻址的(unaddressable) ,所以会报错

Fourth, the question

(X{}).test()    不可寻址 报错了
为什么
(&X{}).test()   能取到地址了?

Guess you like

Origin www.cnblogs.com/xiaobaiskill/p/10936481.html