没有身体的功能是什么意思?

时间:2013-02-18 14:47:53

标签: go abstract-methods

我正在阅读包time的代码,然后我想知道func After(d Duration) <-chan Time是如何工作的。

我发现代码如下:

func After(d Duration) <-chan Time {
    return NewTimer(d).C
}

func NewTimer(d Duration) *Timer {
    c := make(chan Time, 1)
    t := &Timer{
        C: c,
        r: runtimeTimer{
            when: nano() + int64(d),
            f:    sendTime,
            arg:  c,
        },
    }
    startTimer(&t.r)
    return t
}

所以我找到了startTimer的定义 - 函数startTimer没有函数体是如此奇怪。

func startTimer(*runtimeTimer)

我想知道:

  1. startTimer
  2. 的真实代码在哪里
  3. 为什么“抽象方法”可以存在
  4. 为什么Go的作者这样写了
  5. 谢谢!

1 个答案:

答案 0 :(得分:25)

1)该函数定义为here

// startTimer adds t to the timer heap.
//go:linkname startTimer time.startTimer
func startTimer(t *timer) {
    if raceenabled {
        racerelease(unsafe.Pointer(t))
    }
    addtimer(t)
}

2)Function declarations

  

函数声明可以省略正文。这样的声明为Go外部实现的函数提供了签名,例如汇编例程。

3)并非每种编程语言都能完全表达自己的运行时(例如,C可以)。 Go运行时和标准库的一部分在C中,部分在汇编中,而另一些在.goc中,这是一个没有很好记录的Go和C混合。