我是 Go 编程的新手,想知道它们之间有什么区别(如果有的话)
一个。
func DoSomething(a *A) {
b = a
}
b.
func DoSomething(a A) {
b = &a
}
最佳答案
如果你真的想问那些 b
的区别是什么,一个是指向作为参数传递给 DoSomething
的对象的指针,另一个是指向作为参数传递给 DoSomething
的对象副本的指针。
https://play.golang.org/p/ush0hDZsdE
type A struct {
f string
}
func DoSomethingPtr(a *A) {
b := a
b.f = "hi"
}
func DoSomething(a A) {
b := &a
b.f = "hey"
}
func main() {
x := A{"hello"}
DoSomething(x)
fmt.Println(x)
DoSomethingPtr(&x)
fmt.Println(x)
}
关于go - golang中的这些函数有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34803784/