英文:
Why can't I import pkg "builtin"?
问题
cat test.go
package main
import "builtin"
func main() {
return
}
go run test.go
找不到导入包:"builtin"
我只是好奇,因为该文件存在并且已经正确打包,但是无法像其他包一样导入。
/usr/local/go/src/pkg/builtin/builtin.go
英文:
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
答案1
得分: 5
你不需要导入它,它会默认导入。
从http://golang.org/pkg/builtin:
包builtin提供了Go预声明标识符的文档。这里所记录的项目实际上并不在builtin包中,但是它们在这里的描述允许godoc为语言的特殊标识符提供文档。
如果你查看http://golang.org/src/pkg/builtin/builtin.go的内容,你会注意到只有声明:
// copy内置函数将源切片的元素复制到目标切片中。(作为特殊情况,它还可以将字符串的字节复制到字节切片中。)源和目标可以重叠。copy返回复制的元素数,这将是len(src)和len(dst)中的最小值。
func copy(dst, src []Type) int
正如@Anonymous所说,编译器会跳过它:
http://golang.org/src/cmd/go/build.go?#L558
如果p.Standard为真,则:
switch p.ImportPath {
case "builtin", "unsafe":
// 假包 - 无需构建。
return a
}
// gccgo标准库也是“假”的。
if _, ok := buildToolchain.(gccgoToolchain); ok {
// cgo需要目标名称。
a.target = p.target
return a
}
英文:
You don't need to import it. Is imported by default.
From 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.
from golang.org/pkg/builtin)
If you take a look at the content of http://golang.org/src/pkg/builtin/builtin.go
You will notice that there are only declarations
// 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
and as @Anonymous says the compiler skips it:
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
}
}
答案2
得分: 4
当你导入一个包时,编译器(至少是gc编译器)会搜索已经编译好的包。
你可以在源代码中看到这段代码:http://golang.org/src/cmd/gc/lex.c?#L578
特别要注意的是,它不会搜索.go文件:这些文件被假定为已经构建好了。这对于Go来说是一个很大的优势,与C++相比,每个包只需要编译一次,依赖于它的代码可以使用已经编译好的版本。
那么为什么"builtin"包从来没有被构建过,即使它作为一个包存在呢?嗯,这是因为在构建源文件之前,它被特殊处理为被忽略的一部分:http://golang.org/src/cmd/go/build.go?#L558
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论