通过将指针传递给Go中的函数来获取不同的值 [英] Getting different values by passing Pointers to a function in Go

查看:168
本文介绍了通过将指针传递给Go中的函数来获取不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想传递一个指向函数的指针,并通过这样做来改变该指针指向的结构体的值。我通常会通过取消引用指针来实现这一点:

 类型测试结构{值int} 
func main(){
var i Test = Test {2}
var p * Test =& i
f(p)
println(i.Value)// 4
}
func f(p * Test){
* p = Test {4}
}

我的问题是,为什么这段代码没有改变值

  type Test struct {Value int} 
func main(){
var i Test = Test {2}
var p * Test =& i
f(p)
println(i.Value) // 2
}
func f(p * Test){
//?
p =& Test {4}
}



 类型测试结构{值int} 
func main(){
var i Test = Test { 2}
var p * Test =& i
f(p)
println(i.Value)// 4
}
func f(p * Test) {
p.Value = 4
}


解决方案

因为这一行:

  p =& Test {4} 

仅为 p 变量赋值一个新的指针值。在 f()函数中, p 仅仅是一个局部变量。通过为 p 分配任何新值,您只需更改局部变量的值,不是指向的值。



f()中的 p 局部变量与 p main()中的局部变量。如果您在 f()中更改 p ,它不会改变 p 在$(code> main()中(并且它也不会改变指向的结构值)。



你的第二个例子:

  p.Value = 4 

这是一个简写:

 (* p).Value = 4 

这会改变指向的值,因此您会在 f()返回。

注意:

正如附注所示,如果在 main()函数中,您将传递 p main()这是一个指针)中的局部变量)到函数 f(),您可以修改存储在main的 p 中的地址:

  func f(p **测试){
* p =& Test {4}
}

并从 main()中调用它:

  var i Tes t = Test {2} 
var p * Test =& i
f(& p)
println(i.Value)// 2 - 注意'i'不会改变!
println(p.Value)// 4 - 只有'p'中的地址指向在f()中创建的新结构值

但显然传递一个指针 * Test 并修改指向的值( p.Value = 4 )更高效,更方便,更干净。

Say I want to pass a pointer to a function and change the value of a struct which that pointer points to by doing so. I would normally do this by dereferencing the pointer:

type Test struct { Value int}
func main() {
   var i Test = Test {2}
   var p *Test = &i
   f(p)
   println(i.Value)  // 4
}
func f(p *Test) {
   *p = Test{4}
}

My question is, why this code doesn't change the value

type Test struct { Value int}
func main() {
   var i Test = Test {2}
   var p *Test = &i
   f(p)
   println(i.Value)  // 2
}
func f(p *Test) {
   // ?
   p = &Test{4}
}

while this one does:

type Test struct { Value int}
func main() {
   var i Test = Test {2}
   var p *Test = &i
   f(p)
   println(i.Value)  // 4
}
func f(p *Test) {
   p.Value = 4
}

解决方案

Because this line:

p = &Test{4}

Just assigns a new pointer value to the p variable. Inside the f() function, p is just a local variable. By assigning any new value to p, you are just changing the value of the local variable and not the pointed value.

The p local variable in f() has nothing to do with the p local variable in main(). If you change p in f(), it will not change p in main() (and it won't change the pointed struct value either).

In your second example:

p.Value = 4

It is a shorthand for:

(*p).Value = 4

This changes the pointed value, hence you will observe the change when f() returns.

Note:

Just as a side note, if in your main() function you would pass the address of p (the local variable in main() which is a pointer) to function f(), you could modify the address stored in main's p:

func f(p **Test) {
    *p = &Test{4}
}

And from main(), call it like:

var i Test = Test{2}
var p *Test = &i
f(&p)
println(i.Value) // 2 - Note that 'i' won't change!
println(p.Value) // 4 - Only the address in 'p' to the new struct value created in f()

But obviously passing a single pointer *Test and modifying the pointed value (p.Value = 4) is more efficient, much more convenient and much cleaner.

这篇关于通过将指针传递给Go中的函数来获取不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆