没有实体的功能是什么意思? [英] What does a function without body mean?

查看:34
本文介绍了没有实体的功能是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm reading the code that package time, and then I want to know how the func After(d Duration) <-chan Time works.

我发现代码如下:

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没有函数体.

So I found the definition of startTimer - it's so weird that function startTimer does not have a function body.

func startTimer(*runtimeTimer)

我想知道:

  1. startTimer的真正代码在哪里
  2. 为什么这里可以存在抽象方法"
  3. 为什么 Go 的作者这样写

谢谢!

推荐答案

  1. 函数定义在这里:

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

  • 函数声明:

    函数声明可以省略主体.这样的声明为在 Go 之外实现的函数提供了签名,例如汇编例程.

    A function declaration may omit the body. Such a declaration provides the signature for a function implemented outside Go, such as an assembly routine.

  • 并非每种编程语言都可以完全表达自己的运行时(例如,C 可以).Go 运行时和标准库的一部分在 C 中,部分在汇编中,而其他一些在 .goc 中,这是 Go 和 C 的未充分记录的混合体.

  • Not every programming language can express its own runtime entirely (C can, for example). Parts of the Go runtime and the standard library are in C, parts are in assembly while some other are in .goc, which is a not well documented hybrid of Go and C.

    这篇关于没有实体的功能是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆