英文:
Why "go build" cannot find package?
问题
我安装了一个test0
包到$gopath\pkg\windows_386\hello\test0.a
,但是当我构建一个依赖于test0
包的主包时,编译器报错:import "hello/test0": 找不到包
。
为什么会发生这种情况?
我有两个Go文件:
$gopath/src/hello.go
package main
import (
"fmt"
"hello/test0"
)
func main() {
fmt.Println(test0.Number)
}
$gopath/src/hello/test0/test0.go
package test0
const (
Number int = 255
)
首先,我运行了go install hello/test0
,它生成了$gopath\pkg\windows_386\hello\test0.a
然后,我删除了目录$gopath/src/hello
最后,我运行了go build hello.go
,编译器报错hello.go:5:2: import "hello/test0": 找不到包
英文:
I installed a test0
package to $gopath\pkg\windows_386\hello\test0.a
, but when i build a main package which depends on the test0
package, the compiler says: import "hello/test0": cannot find package
.
why this happens?
I have two go file:
$gopath/src/hello.go
package main
import (
"fmt"
"hello/test0"
)
func main() {
fmt.Println(test0.Number)
}
$gopath/src/hello/test0/test0.go
package test0
const (
Number int = 255
)
At first, i run go install hello/test0
, and it generated $gopath\pkg\windows_386\hello\test0.a
then, i delete the directory $gopath/src/hello
finally, i run go build hello.go
, and the compiler sayed hello.go:5:2: import "hello/test0": cannot find package
答案1
得分: 3
这似乎在目前是不正常的可能性:https://code.google.com/p/go/issues/detail?id=2775
也许在Go1.1版本中可能会实现。
Dave提供了一个(我没有测试过的)技巧:
> 对于一个名为“hello”的包,go工具将在$GOPATH/src/hello中查找.go源文件,并且只有在.a文件的时间戳早于.go文件的最新时间戳时才会重新构建。欺骗它只接受.a文件的简单方法是在正确的src目录中放置一个虚拟的.go文件,并将其时间戳设置为早于.a文件的时间戳。
(这是一个社区回答,使用了golang-nuts上的说法)。
英文:
That doesn't seem to be normally possible for the moment : https://code.google.com/p/go/issues/detail?id=2775
Maybe for Go1.1
A trick (that I didn't test) by Dave :
> For a package called "hello", the go tool will look for .go sources in
> $GOPATH/src/hello, and only rebuild if the timestamp of the .a file is
> before the latest timestamp of the .go files. An easy way to fool it
> into accepting just the .a file is to drop a dummy .go file in the
> correct src directory and set its timestamp to before that of the .a
> file.
(this is a community answer, using what is said on golang-nuts).
答案2
得分: 0
你为什么删除了源代码?go工具中的构建命令用于构建一个包及其所有依赖项。为了做到这一点,它会检查包的源代码,以确定是否需要由于更改而重新构建。如果找不到源代码,它将视为未安装。
如果你真的只想处理二进制分发,你需要直接使用编译器和链接器。你可以在这里找到关于它们的文档:http://golang.org/cmd/
英文:
Why did you delete the sources? The build command in the go tool is for building a package and all it's dependencies. To do this it checks the sources of the packages to see if they need to be built due to changes. If it can't find them it will treat them as if they are not installed.
If you really want to deal with just the binary distributions you will need to use the compiler and linkers directly. You can find documentation on those here: http://golang.org/cmd/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论