如何打印Go对象的指针值?指针值是什么意思? [英] How do I print the pointer value of a Go object? What does the pointer value mean?
问题描述
这可能是一个非常棒的模式,愚蠢的问题,但我只是想实验一下,看看我是否仍然在同一个对象上工作,或者我已经做了它的副本(通过值传递它)。
有没有办法打印一个对象的指针(或内部id,如果指针值被gc改变)?
package main
import(runtime)
类型结构{
数字int
队列chan int
}
func gotest(s * Something,done chan bool){
println(from gotest:)
println(& s)
for num:= range s.queue {
println(num)
s.number = num
}
完成< - true
}
func main(){
runtime.GOMAXPROCS(4)
s:= new(Something)
println(& s)
s.queue = make(chan int)
do ne:= make(chan bool)
go gotest(s,done)
s.queue< - 42
close(s.queue)
< - done
println(& s)
println(s.number)
}
< (bg编译版本):
0x4930d4
from gotest:
0x4974d8
42
0x4930d4
42
为什么指针值从内去例行程序显示不同的值?原始对象上的数量确实发生了变化,因此它与同一个对象一起工作。有没有办法看到持久对象ID?
Go函数参数是按值传递的。
首先,放弃您示例中不相关的部分,以便我们可以轻松看到您仅仅通过价值传递参数。例如,
包主
导入fmt
func byval(q * int){
fmt.Printf(3. byval -q%T:& q =%pq =& i =%p * q = i =%v \\ n, q,& q,q,* q)
* q = 4143
fmt.Printf(4. byval -q%T:& q =%pq =& i =%p * q = i =%v \ n,q,& q,q,* q)
q = nil
}
func main(){
i:= int(42)
fmt.Printf(1. main - i%T:& i =%pi =%v \ n,i,& i,i)
p:=& i
fmt.Printf(2. main -p%T:& p =%pp =& i =%p * p = i =%v \\ n,p ,& p,p,* p)
byval(p)
fmt.Printf(5. main - p%T:& p =%pp =& i =%p * p = i =%v \ n,p,& p,p,* p)
fmt.Printf(6. main - i%T:& i =%pi =%v' \\ n,i,& i,i)
}
输出: p>
1。 main - i int:& i = 0xf840000040 i = 42
2. main - p * int:& p = 0xf8400000f0 p =& i = 0xf840000040 * p = i = 42
3 。byval -q * int:& q = 0xf8400000d8 q =& i = 0xf840000040 * q = i = 42
4. byval -q * int:& q = 0xf8400000d8 q =& i = 0xf840000040 * q = i = 4143
5. main - p int:& p = 0xf8400000f0 p =& i = 0xf840000040 * p = i = 4143
6. main - i int: & i = 0xf840000040 i = 4143
在函数 在函数 I n函数 在函数 在函数 main
, i
是一个 int
变量在内存位置(& i
= )
) 0xf800000040
与一个初始值( i
) 42
main
中, p
是指向内存位置(& p
) 0xf8000000f0的
与一个值( int
变量的指针 p
= & i
) 0xf800000040
,它指向一个 int
值( * p
= i
) 42
。
main
, byval(p)
是一个函数调用,它将值( p =
)& i
) 0xf800000040
c>& p 0xf8000000f0
到函数 byval
参数
在内存位置(& q
) 0xf8000000d8
。换句话说,内存分配给 byval
参数 q
,并且 main
byval
参数 p
被分配给它; p
和 q
的值最初是相同的,但变量 p
和 q
不同。
byval $ c使用指针
q
( * int
),它是指针的副本, p (
* int
),integer * q
( i
)被设置为一个新的int值 4143
。在返回之前结束。指针 q
设置为 nil
(零值),这对 p没有影响因为
q
是副本。
main ,
) p
是指向内存位置< int
变量的指针c>& p 0xf8000000f0
的值( p
= <$ c $指向新的 int
值(<$ c>& i 0xf800000040
c $ c> * p i
) 4143
。
在函数 main
中, i
是 int
变量在内存位置(& i
) 0xf800000040
带有最终值(<$ c
在您的示例中,函数<$ c> 用作函数
变量 gotest
调用的参数的c $ c> main s
与 gotest
参数 s
不同。它们具有相同的名称,但具有不同范围和内存位置的不同变量。函数参数 s
隐藏函数调用参数 s
。这就是为什么在我的例子中,我分别命名了参数和参数变量 p
和 q
,以强调它们之间的差异。 p>
在你的例子中,( 你应该接受mkb的忠告并停止使用 指针当它们指向相同的内存位置时具有相同的值;当指向不同的内存位置时,指针有不同的值。 I am just playing around with Go and do not yet have a good mental model of when structs are passed by value or by reference. This may be a very dumb question but I just want to experiment a bit and see if I am still working on the same object or I have made a copy of it (passed it by value). Is there a way to print the pointer (or internal id if pointer value is changed by gc) of an object? gives on my windows (8g compiled version): Why does the pointer value from within the go routine show a different value? The quantity on the original object did get changed so it was working with the same object. Is there a way to see an object id that is persistent? Go function arguments are passed by value. First, let's discard the irrelevant parts of your example, so that we can easily see that you are merely passing an argument by value. For example, Output: In function In function In function In function In function In function In your example, the function In your example, ( You should take mkb's sage advice and stop using Pointers have the same value when they point to the same memory location; pointers have different values when they point to different memory locations. 这篇关于如何打印Go对象的指针值?指针值是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!& s
) 0x4930d4
是变量 s
在函数 main
中用作函数调用参数的地址 gotest(s,done)
和 0x4974d8
是函数的内存地址, gotest
参数 s
。如果在函数 gotest
的末尾设置参数 s = nil
,它对变量<$ c $没有影响在 main
中有c> s ; 在
是不同的内存位置。就类型而言,主要
和 s
中的 s
> gotest & s
是 ** Something
, s
* Something
, * s
是 Something
。 & s
是指向(内存地址) s
的指针,位置)类型 Something
的匿名变量。就值而言, main。& s!= gotest。& s
, main.s == gotest.s
, main。* s == gotest。* s
和 main.s.number == gotest.s.number $ c
println(& s)
。使用 fmt
包,例如,
fmt.Printf( %v%p%v \ n,& s,s,* s)
package main
import ( "runtime" )
type Something struct {
number int
queue chan int
}
func gotest( s *Something, done chan bool ) {
println( "from gotest:")
println( &s )
for num := range s.queue {
println( num )
s.number = num
}
done <- true
}
func main() {
runtime.GOMAXPROCS(4)
s := new(Something)
println(&s)
s.queue = make(chan int)
done := make(chan bool)
go gotest(s, done)
s.queue <- 42
close(s.queue)
<- done
println(&s)
println(s.number)
}
0x4930d4
from gotest:
0x4974d8
42
0x4930d4
42
package main
import "fmt"
func byval(q *int) {
fmt.Printf("3. byval -- q %T: &q=%p q=&i=%p *q=i=%v\n", q, &q, q, *q)
*q = 4143
fmt.Printf("4. byval -- q %T: &q=%p q=&i=%p *q=i=%v\n", q, &q, q, *q)
q = nil
}
func main() {
i := int(42)
fmt.Printf("1. main -- i %T: &i=%p i=%v\n", i, &i, i)
p := &i
fmt.Printf("2. main -- p %T: &p=%p p=&i=%p *p=i=%v\n", p, &p, p, *p)
byval(p)
fmt.Printf("5. main -- p %T: &p=%p p=&i=%p *p=i=%v\n", p, &p, p, *p)
fmt.Printf("6. main -- i %T: &i=%p i=%v\n", i, &i, i)
}
1. main -- i int: &i=0xf840000040 i=42
2. main -- p *int: &p=0xf8400000f0 p=&i=0xf840000040 *p=i=42
3. byval -- q *int: &q=0xf8400000d8 q=&i=0xf840000040 *q=i=42
4. byval -- q *int: &q=0xf8400000d8 q=&i=0xf840000040 *q=i=4143
5. main -- p *int: &p=0xf8400000f0 p=&i=0xf840000040 *p=i=4143
6. main -- i int: &i=0xf840000040 i=4143
main
, i
is an int
variable at memory location (&i
) 0xf800000040
with an initial value (i
) 42
.main
, p
is a pointer to an int
variable at memory location (&p
) 0xf8000000f0
with a value (p
=&i
) 0xf800000040
which points to an int
value (*p
=i
) 42
.main
, byval(p)
is a function call which assigns the value (p
=&i
) 0xf800000040
of the argument at memory location (&p
) 0xf8000000f0
to the function byval
parameter q
at memory location (&q
) 0xf8000000d8
. In other words, memory is allocated for the byval
parameter q
and the value of the main
byval
argument p
is assigned to it; the values of p
and q
are initially the same, but the variables p
and q
are distinct.byval
, using pointer q
(*int
), which is a copy of pointer p
(*int
), integer *q
(i
) is set to a new int value 4143
. At the end before returning. the pointer q
is set to nil
(zero value), which has no effect on p
since q
is a copy.main
, p
is a pointer to an int
variable at memory location (&p
) 0xf8000000f0
with a value (p
=&i
) 0xf800000040
which points to a new int
value (*p
=i
) 4143
.main
, i
is an int
variable at memory location (&i
) 0xf800000040
with a final value (i
) 4143
.main
variable s
used as an argument to the function gotest
call is not the same as the function gotest
parameter s
. They have the same name, but are different variables with different scopes and memory locations. The function parameter s
hides the function call argument s
. That's why in my example, I named the argument and parameter variables p
and q
respectively to emphasize the difference.&s
) 0x4930d4
is the address of the memory location for the variable s
in function main
that is used as an argument to the function call gotest(s, done)
, and 0x4974d8
is the address of the memory location for the function gotest
parameter s
. If you set parameter s = nil
at the end of function gotest
, it has no effect on variable s
in main
; s
in main
and s
in gotest
are distinct memory locations. In terms of types, &s
is **Something
, s
is *Something
, and *s
is Something
. &s
is a pointer to (address of memory location) s
, which is a pointer to (address of memory location) an anonymous variable of type Something
. In terms of values, main.&s != gotest.&s
, main.s == gotest.s
, main.*s == gotest.*s
, and main.s.number == gotest.s.number
.println(&s)
. Use the fmt
package, for example,fmt.Printf("%v %p %v\n", &s, s, *s)