英文:
Empty function in golang
问题
我正在查看Go源代码,下面是来自sleep.go(time包)的一段代码片段:
package time
// Sleep pauses the current goroutine for at least the duration d.
// A negative or zero duration causes Sleep to return immediately.
func Sleep(d Duration)
// runtimeNano returns the current value of the runtime clock in nanoseconds.
func runtimeNano() int64
为什么func Sleep(d Duration)
没有任何实现呢?我在哪里可以找到Sleep函数的确切实现?
编辑:可以在@DaveC提供的两个链接中找到答案。然而,请阅读下面的评论,了解为什么即使在引导Go(1.5)之后,空函数也不能用Go替代的解释。
英文:
Was looking at the source code go source code, below is a snippet from sleep.go (package time):
package time
// Sleep pauses the current goroutine for at least the duration d.
// A negative or zero duration causes Sleep to return immediately.
func Sleep(d Duration)
// runtimeNano returns the current value of the runtime clock in nanoseconds.
func runtimeNano() int64
How is it possible that func Sleep(d Duration) doesn't have any implementation? Where can I find the exact implementation of Sleep function?
Edit: The answer can be found in the 2 links provided by @DaveC. However please read the comments below to see the explanation about why empty function can't be replaced with Go even after bootstrapping Go (1.5)
答案1
得分: 2
考虑到Sleep()
的功能(它会将一个golang线程暂停一段时间),你无法在Go语言中直接表达这个功能,所以它可能是以某种编译器内置方式实现的。
哇,这里有很多负评。看起来Sleep
实际上是用汇编语言实现的。你可以在这里找到更多信息。
英文:
Considering what Sleep()
does (it deschedules a golang thread for a given amount of time), you can't express that it in go, so it's probably implemented as some kind of compiler instrinsic.
Whoa, lots of downvotes here. It looks like Sleep
is in fact implemented in assembly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论