【发布时间】:2018-07-05 11:37:41
【问题描述】:
在标准库中,src/time/sleep.go 有以下内容:
// Interface to timers implemented in package runtime.
// Must be in sync with ../runtime/runtime.h:/^struct.Timer$
type runtimeTimer struct {
i int
when int64
period int64
f func(interface{}, uintptr) // NOTE: must not be closure
arg interface{}
seq uintptr
}
func startTimer(*runtimeTimer)
startTimer的参数类型为*runtimeTimer。 startTimer实现在src/runtime/time.go中,如下:
// Package time knows the layout of this structure.
// If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
// For GOOS=nacl, package syscall knows the layout of this structure.
// If this struct changes, adjust ../syscall/net_nacl.go:/runtimeTimer.
type timer struct {
i int // heap index
// Timer wakes up at when, and then at when+period, ... (period > 0 only)
// each time calling f(arg, now) in the timer goroutine, so f must be
// a well-behaved function and not block.
when int64
period int64
f func(interface{}, uintptr)
arg interface{}
seq uintptr
}
// startTimer adds t to the timer heap.
//go:linkname startTimer time.startTimer
func startTimer(t *timer) {
if raceenabled {
racerelease(unsafe.Pointer(t))
}
addtimer(t)
}
这里startTimer的参数类型是*timer。
*timer 和 *runtimeTimer 是不同的类型。因为根据golangspec:
如果两个指针类型具有相同的基类型,则它们是相同的
和
定义的类型总是不同于任何其他类型
timer 和 runtimeTimer 都是定义类型,所以 *timer 和 *runtimeTimer 是不同的类型。
根据可分配性rule,函数调用中的参数分配也不应该起作用。
谁能给我解释一下?
谢谢
【问题讨论】: