Why does the golang compiler not always get the address of the value?

for example, the golang compiler does not always get the address of the value


type Integer int 

func (i *Integer) pretty(){
 
}

Integer(25).pretty()//

ask for advice, why do you say so, under what circumstances can"t you find the pointer, and what is the principle

Mar.20,2021

because Integer (25). Pretty () will be optimized to an integer (constant) 25 call pretty function .

you should add a temporary variable, like this

n := Integer(25)
n.pretty()

  • it is reasonable that a constant cannot take a pointer. If a constant can get a pointer, it means that it can be modified and cannot be called a constant.
  • here is a blog post about go addressable .

golang so few people, only a few of you can be invited, thank you!

Menu