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 you can not import, like other packages.

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

+4
source share
2 answers

When importing a package, the compiler (or at least the gc compiler) looks 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 does not look for .go files: it is assumed that they are already built. This is a big win for comparison compared to, for example, C ++, because each package can be compiled once, and the code that depends on it can use the already compiled version.

"" , ? , , , : http://golang.org/src/cmd/go/build.go?#L558

+4

. .

http://golang.org/pkg/builtin:

Package builtin provides documentation for Go predeclared identifiers. The items documented here are not actually in package builtin but their descriptions here allow godoc to present documentation for the language special identifiers. 

golang.org/pkg/builtin)

http://golang.org/src/pkg/builtin/builtin.go ,

    // The copy built-in function copies elements from a source slice into a
    // destination slice. (As a special case, it also will copy bytes from a
    // string to a slice of bytes.) The source and destination may overlap. Copy
    // returns the number of elements copied, which will be the minimum of
    // len(src) and len(dst).
    func copy(dst, src []Type) int

@Anonymous , : http://golang.org/src/cmd/go/build.go?#L558

       if p.Standard {
            switch p.ImportPath {

            case "builtin", "unsafe":
                // Fake packages - nothing to build.
            return a
            }

            // gccgo standard library is "fake" too.
            if _, ok := buildToolchain.(gccgoToolchain); ok {
                // the target name is needed for cgo.
                a.target = p.target
                return a
            }
        }
+4

Source: https://habr.com/ru/post/1542912/


All Articles