2

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)

srock
  • 403
  • 1
  • 8
  • 17
  • 2
    Also see [What does a function without body mean?](http://stackoverflow.com/questions/14938960/what-does-a-function-without-body-mean) – Dave C Apr 14 '15 at 21:57
  • Thanks for pointing to those 2 questions, it did help @Dave C. So does it mean that once golang is bootstrapped (in 1.5, is it possible to replace ASM with Go) these empty functions will be replaced with actual implementation? I have don't have much clue about compilers. I'm sorry if this comment doesn't make sense. – srock Apr 14 '15 at 22:04
  • I expect/guess all the assembler will remain. If it wasn't time critical or otherwise easier/necessary to have it in assembler it likely would already have been Go (or C, which as you say has all become Go for 1.5). – Dave C Apr 14 '15 at 22:06
  • Assembly still needs to be written using architecture specific machine instructions. Regardless of what the "assembler" is written in, you won't be able to replace the actual logic of the assembly code with Go. Look at the `*$GOOS_$GOARCH.s` files in the runtime source. – JimB Apr 14 '15 at 22:14

1 Answers1

2

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.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • 2
    The problem with the answer was because there's no need to speculate, it's written right in the [documentation](http://golang.org/ref/spec#Function_declarations) – JimB Apr 14 '15 at 22:00
  • Fair enough. However Sleep is an interesting example of such a function because it can't be written in go. – Rhythmic Fistman Apr 14 '15 at 22:07