为什么使用值传入,输出的是正确的,但使用指针传入,再取值,获得的值却是一样的呢????
```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呢?????
有疑问加站长微信联系(非本文作者)