dswy34539 2014-06-02 12:29
浏览 123
已采纳

为什么我不能导入pkg“内置”?

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

  • 写回答

2条回答 默认 最新

  • dougan1205 2014-06-02 13:08
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算