我是 Golang 世界的新手。我试图理解自动引用的概念。


我对以下程序的困惑?第一个和第二个有什么区别


函数 main 的第一行

age(20).print()

二三线

myAge := age(20)

myAge.print()

完整程序如下:


package main

import "fmt"


type age uint8


func (a *age) print() {

  fmt.Println(a)

}



func main() {

  age(20).print() // this line throws error


  // however, the below line works properly

  myAge := age(20)

  myAge.print()

}

请帮助理解这两种方法之间的区别。我假设他们都是一样的。