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

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

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.

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

b.

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

答案1

得分: 3

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

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)
}
英文:

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

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)
}

答案2

得分: 0

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

package main

type A struct{
    x int
}
var b *A

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

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

func main() {
    var a = A{3}
    println(&a)
    d(&a)
    println(b)
    e(a)
    println(b)
}

有趣的是,如果将类型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

package main

type A struct{
	x int
}
var b *A

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

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

func main() {
	var a = A{3}
	println(&a)
	d(&a)
	println(b)
	e(a)
	println(b)
}

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在每个函数中都会被赋予不同的值。这些值是不同的,因为一个是传递了一个复制的值,而另一个是传递了指向内存中原始值的指针。

package main

import "fmt"

type A string

func DoSomethingPtr(a *A) {
    fmt.Println(a)
}

func DoSomething(a A) {
    fmt.Println(&a)
}

func main() {
    x := A("hello")
    DoSomething(x)
    DoSomethingPtr(&x)
}

这里是可执行的证明。

英文:

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.

package main

import "fmt"

type A string

func DoSomethingPtr(a *A) {
    fmt.Println(a)
}

func DoSomething(a A) {
    fmt.Println(&a)
}

func main() {
    x := A("hello")
    DoSomething(x)
    DoSomethingPtr(&x)
}

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:

确定