运行:
package main
import "fmt"
type Point struct {
X, Y float64
}
func (p *Point) Describe() {
fmt.Printf("(%v, %v)\n", p.X, p.Y)
}
func main() {
(&Point{1, 2}).Describe()
Point{1, 2}.Describe()
}
报 2 个编译错误,都是由 Point{1, 2}.Describe()
引起的:
./prog.go:15:13: cannot call pointer method on Point literal
./prog.go:15:13: cannot take the address of Point literal
“cannot take the address of Point literal” 很奇怪,上一行中 &Point{1,2}
明明取址了,而且也没报错。Spec 中也说了可以取址。这句该怎么理解?是不是为了强行解释“cannot call pointer method on Point literal”?