英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论