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

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

问题描述

我正在阅读包含 time 的代码,然后我想知道 func After(d持续时间)< -chan时间起作用。



我发现代码如下:

  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 {
当:nano()+ int64(d),
f:sendTime,
arg:c

$ b $ startTimer
return t

$ >

所以我找到了 startTimer 的定义 - 它很奇怪,函数 startTimer 没有函数体。

  func startTimer(* runtimeTimer)

我想知道:


  1. 真正的代码 startTimer

  2. 为什么一个抽象方法可以在这里存在

  3. 为什么Go的作者是这样写的

谢谢s!

解决方案

1)该函数被定义为 here $ b

  / / startTimer将t添加到定时器堆中。 
// go:linkname startTimer time.startTimer
func startTimer(t * timer){
如果raceenabled {
racerelease(unsafe.Pointer(t))
}
addtimer(t)
}



<2> 函数声明


函数声明可能会遗漏身体。这样的声明提供了在Go之外实现的函数的签名,比如汇编例程。

3)并非每种编程语言都可以表达它自己的运行时间完全(例如C可以)。 Go运行时和标准库中的一部分是C语言,部分是汇编语言,而另外一些则在 .goc 中,这是Go和C.

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

I found the code follows:

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
}

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

func startTimer(*runtimeTimer)

I want to know that :

  1. Where is the real code of startTimer
  2. Why an "abstract method" can exists here
  3. Why the author of Go wrote it like this

Thanks!

解决方案

1) The function is defined 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:

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

3) 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天全站免登陆