```go
package main
import (
"fmt"
"time"
)
func main() {
var num = 10
var p = &num
c := make(chan int)
go func() {
time.Sleep(time.Second)
c <- *p //-----> 11
//c <- num //----->10
}()
time.Sleep(2 * time.Second)
num++
fmt.Println(<-c)
fmt.Println(p)
return
}
```
为什么 *p 和num的结果不一样
本问题完结
经过查看chansend1的源码,并且添加了如下注释
```go
func chansend1(c *hchan, elem unsafe.Pointer) {
print("chansend1: chan=", c, ", elem=", elem, "\n")
chansend(c, elem, true, getcallerpc(unsafe.Pointer(&c)))
}
```
增加源代码的打印信息
```go
fmt.Println("num=", &num)
go func() {
time.Sleep(time.Second)
fmt.Println("----------------")
//c <- *p //-------> 11
c <- num //----->10
}()
```
使用 c<-num 时的打印 如下
```
chansend1: chan=0xc042014070, elem=0x4d87b0
num= 0xc04200e0b0
----------------
chansend1: chan=0xc04203c060, elem=0xc042021fa8
10
```
使用c<-*p 时的打印如下
```
chansend1: chan=0xc042014070, elem=0x4d87b0
num= 0xc04200e0b0
----------------
chansend1: chan=0xc04203c060, elem=0xc04200e0b0
11
```
应该可以证明<a href="/user/polaris" title="@polaris">@polaris</a> 假设是成立的
#17
更多评论