gobuilt-in

Why can't I import pkg "builtin"?


cat test.go

package main

import "builtin"

func main() {
    return
}

go run test.go

can't find import: "builtin"

I'm just curious because the file exists and is properly packaged. But can't be imported like other packages.

/usr/local/go/src/pkg/builtin/builtin.go


Solution

  • When you import a package, the compiler (or at least, the gc compiler), searches for the already compiled package.

    You can see this code in the source: http://golang.org/src/cmd/gc/lex.c?#L578

    In particular, it doesn't search for .go files: these are assumed to be already built. This is a big win for go compared to, for example, C++, because each package can be compiled once, and code that depends on it can use the already-compiled version.

    So why doesn't "builtin" ever get built, even though it's there as a package? Well, it's special-cased to be ignored in the part of the code that builds dependencies before building a source file: http://golang.org/src/cmd/go/build.go?#L558