通过将指针传递给Go中的函数来获取不同的值 [英] Getting different values by passing Pointers to a function in 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()中创建的新结构值
但显然传递一个指针 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: My question is, why this code doesn't change the value while this one does:
Because this line: Just assigns a new pointer value to the The In your second example: It is a shorthand for: This changes the pointed value, hence you will observe the change when Note: Just as a side note, if in your And from But obviously passing a single pointer 这篇关于通过将指针传递给Go中的函数来获取不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! * Test
并修改指向的值( p.Value = 4
)更高效,更方便,更干净。
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}
}
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}
}
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
}
p = &Test{4}
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.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).p.Value = 4
(*p).Value = 4
f()
returns.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}
}
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()
*Test
and modifying the pointed value (p.Value = 4
) is more efficient, much more convenient and much cleaner.