为什么使用值传入,输出的是正确的,但使用指针传入,再取值,获得的值却是一样的呢????
```go
package main
import (
"fmt"
"time"
)
func tggg(p *int) {
fmt.Printf("p = %p, v = %d\n", p, *p)
}
func tggg1(t int) {
fmt.Printf("v = %d\n", t)
}
func main() {
number := []int{1, 2, 3, 4, 5}
for _, v := range number {
go tggg1(v)
go tggg(&v)
}
time.Sleep(5*time.Second)
}
```
输出结果:
```
v = 1
p = 0xc000084010, v = 5
v = 2
p = 0xc000084010, v = 5
v = 3
p = 0xc000084010, v = 5
v = 4
p = 0xc000084010, v = 5
v = 5
p = 0xc000084010, v = 5
```
---------------------------------------时间线---------------------------
我有加了个sleep 输出就不一样了,怎么感觉从地址里取值有延时啊
```go
package main
import (
"fmt"
"time"
)
func tggg(p *int) {
fmt.Printf("p = %p, v = %d\n", p, *p)
}
func tggg1(t int) {
fmt.Printf("v = %d\n", t)
}
func main() {
number := []int{1, 2, 3, 4, 5}
for k, v := range number {
// go tggg1(v)
switch k {
case 0:
time.Sleep(1*time.Second)
go tggg(&v)
case 1:
time.Sleep(2*time.Second)
go tggg(&v)
case 2:
time.Sleep(3*time.Second)
go tggg(&v)
case 3:
time.Sleep(4*time.Second)
go tggg(&v)
case 4:
time.Sleep(5*time.Second)
go tggg(&v)
}
// go tggg(&v)
}
time.Sleep(5*time.Second)
}
```
输出结果:
```
= 0xc000084010, v = 2
p = 0xc000084010, v = 3
p = 0xc000084010, v = 4
p = 0xc000084010, v = 5
p = 0xc000084010, v = 5
```
但是为啥没有1 ,却有两个5呢?????
更多评论
啊,我是看了https://studygolang.com/articles/23530#reply1 才知道这个的。。。
我不是很理解啊,虽然传的是指针,但是那个地址存的值不是应该实时变得吗?为啥不变啊
#2
![image.png](https://static.studygolang.com/190924/0f250b302ad712614ea33293f29ec402.png)
目测语法错误,不知道你是怎么编译过去的!!!
#3