这些函数在golang中有什么区别?

huangapple go评论93阅读模式
英文:

What's the difference between these functions in golang?

问题

a.

func DoSomething(a *A) {
b = a
}

这段代码中,函数DoSomething接受一个指向类型A的指针作为参数a。在函数内部,将a赋值给变量b。这意味着b和a指向同一个内存地址,对b的修改也会影响到a。

b.

func DoSomething(a A) {
b = &a
}

这段代码中,函数DoSomething接受一个类型A的值作为参数a。在函数内部,使用取地址符&将a的地址赋值给变量b。这意味着b指向a的地址,对b的修改不会影响到a。

英文:

I'm new to Go programming and wondering what's the difference (if any) there between

a.

  1. func DoSomething(a *A) {
  2. b = a
  3. }

b.

  1. func DoSomething(a A) {
  2. b = &a
  3. }

答案1

得分: 3

如果你实际上是在询问这些b的区别,那么一个是指向作为参数传递给DoSomething的对象的指针,另一个是指向作为参数传递给DoSomething的对象的副本的指针。

  1. type A struct {
  2. f string
  3. }
  4. func DoSomethingPtr(a *A) {
  5. b := a
  6. b.f = "hi"
  7. }
  8. func DoSomething(a A) {
  9. b := &a
  10. b.f = "hey"
  11. }
  12. func main() {
  13. x := A{"hello"}
  14. DoSomething(x)
  15. fmt.Println(x)
  16. DoSomethingPtr(&x)
  17. fmt.Println(x)
  18. }
英文:

If you are actually asking what the difference of those b's are, one is a pointer to the object passed as an argument to DoSomething, and the other is a pointer to a copy of the object passed as an argument to DoSomething.

https://play.golang.org/p/ush0hDZsdE

  1. type A struct {
  2. f string
  3. }
  4. func DoSomethingPtr(a *A) {
  5. b := a
  6. b.f = "hi"
  7. }
  8. func DoSomething(a A) {
  9. b := &a
  10. b.f = "hey"
  11. }
  12. func main() {
  13. x := A{"hello"}
  14. DoSomething(x)
  15. fmt.Println(x)
  16. DoSomethingPtr(&x)
  17. fmt.Println(x)
  18. }

答案2

得分: 0

一般来说,这两个函数会给b赋予不同的值。第二个函数会对参数进行复制,所以函数内部的a通常与传入函数的输入具有不同的内存地址。可以参考这个示例

  1. package main
  2. type A struct{
  3. x int
  4. }
  5. var b *A
  6. func d(a *A) {
  7. b = a
  8. }
  9. func e(a A) {
  10. b = &a
  11. }
  12. func main() {
  13. var a = A{3}
  14. println(&a)
  15. d(&a)
  16. println(b)
  17. e(a)
  18. println(b)
  19. }

有趣的是,如果将类型A设置为空结构体,并初始化var a = A{},你会在println语句中看到相同的b值。

这是因为对于空结构体类型,实际上只能有一个值,并且它是不可变的,所以所有的实例都共享相同的内存地址。

英文:

In general, these two functions will assign different values to b. The second one makes a copy of the argument, and so the a inside the function generally has a different memory address than whatever input is passed into the function. See this playground example

  1. package main
  2. type A struct{
  3. x int
  4. }
  5. var b *A
  6. func d(a *A) {
  7. b = a
  8. }
  9. func e(a A) {
  10. b = &a
  11. }
  12. func main() {
  13. var a = A{3}
  14. println(&a)
  15. d(&a)
  16. println(b)
  17. e(a)
  18. println(b)
  19. }

Interestingly, if you make the type A an empty struct instead, and initialize var a = A{}, you actually see the same value for b in the println statements.

That's because for the empty-struct type, there can only really only ever be 1 value, and its immutable, so all instances of it share the same memory address?

答案3

得分: 0

变量b在每个函数中都会被赋予不同的值。这些值是不同的,因为一个是传递了一个复制的值,而另一个是传递了指向内存中原始值的指针。

  1. package main
  2. import "fmt"
  3. type A string
  4. func DoSomethingPtr(a *A) {
  5. fmt.Println(a)
  6. }
  7. func DoSomething(a A) {
  8. fmt.Println(&a)
  9. }
  10. func main() {
  11. x := A("hello")
  12. DoSomething(x)
  13. DoSomethingPtr(&x)
  14. }

这里是可执行的证明。

英文:

The variable b would be assigned a different value in each function. The values are different because one is passing a copied value and the other is passing a pointer to the original value in memory.

  1. package main
  2. import "fmt"
  3. type A string
  4. func DoSomethingPtr(a *A) {
  5. fmt.Println(a)
  6. }
  7. func DoSomething(a A) {
  8. fmt.Println(&a)
  9. }
  10. func main() {
  11. x := A("hello")
  12. DoSomething(x)
  13. DoSomethingPtr(&x)
  14. }

Here is the executable proof.

huangapple
  • 本文由 发表于 2016年1月15日 11:11:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/34803784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定