cat test.go
package main
import "builtin"
func main() {
return
}
Run Code Online (Sandbox Code Playgroud)
go run test.go
can't find import: "builtin"
Run Code Online (Sandbox Code Playgroud)
我只是好奇,因为文件存在并且已正确打包.但不能像其他包一样导入.
/usr/local/go/src/pkg/builtin/builtin.go
您不需要导入它。默认是导入的。
来自http://golang.org/pkg/builtin:
Package builtin provides documentation for Go's predeclared identifiers. The items documented here are not actually in package builtin but their descriptions here allow godoc to present documentation for the language's special identifiers.
Run Code Online (Sandbox Code Playgroud)
来自 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
Run Code Online (Sandbox Code Playgroud)
正如@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
}
}
Run Code Online (Sandbox Code Playgroud)
当您导入包时,编译器(或至少是 gc 编译器)会搜索已编译的包。
您可以在源代码中看到此代码:http://golang.org/src/cmd/gc/lex.c ?#L578
特别是,它不会搜索 .go 文件:假定这些文件已经构建。与 C++ 等语言相比,这对 Go 来说是一个巨大的胜利,因为每个包都可以编译一次,并且依赖于它的代码可以使用已经编译的版本。
那么为什么“内置”永远不会被构建,即使它作为一个包存在呢?好吧,在构建源文件之前构建依赖关系的代码部分中需要忽略它的特殊情况:http://golang.org/src/cmd/go/build.go ?#L558
| 归档时间: |
|
| 查看次数: |
989 次 |
| 最近记录: |