我想知道在指针上有方法和在值上有方法有什么区别。这两种方法如何在标准结构实例和结构指针上工作。
最佳答案
将接收者定义为值
格式:
func (r T) Xxx() {}
可以通过值或指针调用。
用指针调用时,会自动传递值,(实际上是使用*
获取调用者的值,并传递)。
将接收者定义为指针
格式:
func (r *T) Xxx() {}
原则上,应该只用指针调用,但这不是必需的。
因为当使用值而不是指针调用时,编译器会在可能的情况下处理它:
- 如果该值是可寻址的,(对于 go 中的大多数数据类型都是如此)。
然后编译器将获取地址(通过&
),并自动传递它。
这使得可以直接调用具有值的指针方法,(我猜这在 go 中很常见,它使程序员的生活更轻松)。 - 如果该值不可寻址,(这种情况很少见,但存在)。
然后需要显式传递地址,否则编译时会出错。
例如map
的元素不可寻址。
提示
在定义一个方法时,如果合适的话,指针调用者是首选。
原因:- 它可以修改调用者。
- 它对于复杂的调用者来说更轻量级。
传递给方法的内容取决于方法签名,而不是您如何调用它(这与参数类似)。
- 当声明调用者为指针
(r *T)
时,它传递指针。 - 当将调用者声明为值
(r T)
时,它传递原始调用者的副本。
- 当声明调用者为指针
T
本身不能是指针。
代码
还有,这是我在学习这个特性时写的一段go代码。
它在 main()
中调用的 2 个函数分别测试这 2 个特性。
method_learn.go:
// method - test
package main
import (
"fmt"
"math"
)
type Vertex struct {
x float64
y float64
}
// abs, with pointer caller,
func (v *Vertex) AbsPointer() float64 {
return math.Sqrt(v.x*v.x + v.y*v.y)
}
// scale, with pointer caller,
func (v *Vertex) ScalePointer(f float64) *Vertex {
v.x = v.x * f
v.y = v.y * f
return v
}
// abs, with value caller,
func (v Vertex) AbsValue() float64 {
return math.Sqrt(v.x*v.x + v.y*v.y)
}
// test - method with pointer caller,
func pointerCallerLearn() {
vt := Vertex{3, 4}
fmt.Printf("Abs of %v is %v. (Call %s method, with %s)\n", vt, vt.AbsPointer(), "pointer", "value") // call pointer method, with value,
fmt.Printf("Abs of %v is %v. (Call %s method, with %s)\n\n", vt, (&vt).AbsPointer(), "pointer", "pointer") // call pointer method, with pointer,
// scala, change original caller,
fmt.Printf("%v scale by 10 is: %v (Call %s method, with %s)\n", vt, vt.ScalePointer(10), "pointer", "value") // call pointer method, with value,
fmt.Printf("%v scale by 10 is: %v (Call %s method, with %s)\n", vt, (&vt).ScalePointer(10), "pointer", "pointer") // call pointer method, with pointer,
}
// test - method with value caller,
func valueCallerLearn() {
vt := Vertex{3, 4}
fmt.Printf("Abs of %v is %v. (Call %s method, with %s)\n", vt, (&vt).AbsValue(), "value", "pointer") // call value method, with pointer,
fmt.Printf("Abs of %v is %v. (Call %s method, with %s)\n", vt, vt.AbsValue(), "value", "value") // call value method, with value,
}
func main() {
// pointerCallerLearn()
valueCallerLearn()
}
只需修改 main()
,然后通过 go run method_test.go
运行,然后检查输出以了解其工作原理。
关于Go 的值方法和指针的方法有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54476651/