英文:
go build vs go build file.go
问题
go build
和go build file.go
之间有什么区别?
我问这个问题是因为当我在导入本地包的包上运行go build
时,我会得到以下错误信息。
can't load package: C:\go\src\bug\main.go:3:8: local import "./local_file" in non-local package
然而,当我指定一个文件名时,它可以工作。例如go build main.go
Windows XP上的控制台历史记录。
C:\gopath\src\bug:>go version
go version go1.1 windows/386
C:\gopath\src\bug:>dir
...
<DIR> local_file
55 main.go
...
C:\gopath\src\bug:>type main.go
package main
import _ "./local_file"
func main() {
}
C:\gopath\src\bug:>type local_file\local_file.go
package local_file
import "fmt"
func init() {
fmt.Println("Called: local_file.init()")
}
C:\gopath\src\bug:>go run main.go
Called: local_file.init()
C:\gopath\src\bug:>go build main.go
C:\gopath\src\bug:>dir
...
<DIR> local_file
1,285,120 main.exe
55 main.go
...
C:\gopath\src\bug:>go build
can't load package: C:\gopath\src\bug\main.go:3:8: local import "./local_file" in non-local package
英文:
What's the difference between go build
and go build file.go
?
I'm asking because when I run go build
on a package that imports a local package, then I get this error message.
can't load package: C:\go\src\bug\main.go:3:8: local import "./local_file" in non-local package
However, when I specify a file name it works. Ex go build main.go
Console history on Windows XP.
C:\gopath\src\bug:>go version
go version go1.1 windows/386
C:\gopath\src\bug:>dir
...
<DIR> local_file
55 main.go
...
C:\gopath\src\bug:>type main.go
package main
import _ "./local_file"
func main() {
}
C:\gopath\src\bug:>type local_file\local_file.go
package local_file
import "fmt"
func init() {
fmt.Println("Called: local_file.init()")
}
C:\gopath\src\bug:>go run main.go
Called: local_file.init()
C:\gopath\src\bug:>go build main.go
C:\gopath\src\bug:>dir
...
<DIR> local_file
1,285,120 main.exe
55 main.go
...
C:\gopath\src\bug:>go build
can't load package: C:\gopath\src\bug\main.go:3:8: local import "./local_file" in non-local package
答案1
得分: 9
我在golang的Google Group网站上提出了这个问题,这是其中一个回答:
go build file.go
(即使file.go
在GOPATH
中)并不会构建工作区中的一个包,所以它不遵循那个规则。缺点是你必须命名所有的*.go
文件,你无法获得go
命令的一些自动行为(例如:测试和安装),而且你无法构建一个需要安装的包,只能构建可执行文件。此外,它也无法通过go get
获取。“
go build file.go
”只应该用于简单的单源文件二进制文件,你不打算分发或重用其中的代码。即使是这样,按照go工具的约定通常更简单更快:每个文件夹一个包(反之亦然),不使用相对导入(永远不要使用),“go run file.go
”也遵循相同的逻辑,但更不推荐使用。这两种模式都是为特定用途而设计的,应该避免在常规程序和包中使用。
所以事实证明,Google Go要求包的导入路径相对于环境变量GOROOT
或GOPATH
中的src
目录。
因此,以下是使用go build
构建程序的修复方法。
注意:GOPATH
设置为C:\gopath
将main.go
从以下代码更改为:
package main
import _ "bug/local_file"
func main() {
}
英文:
I asked this question on the google group site for golang and here's one of the responses.
> go build file.go
(even if file.go
is in GOPATH
) isn't building a
> package in a workspace, so it follows doesn't follow that rule. The
> downside is that you have to name all the *.go
files, you don't get
> some of the automatic behaviour of the go
command (eg: testing &
> installing), and you can't build a package (which requires
> installing), only executables. Also, it's not go-gettable.
>
> The "go build file.go
" should only ever be used for simple one source
> file binaries that you don't intend to ever distribute or re-use code
> from. Even then, it's often simpler and faster to follow the go tool's
> conventions: one package per folder (and vice versa) no relative
> imports (ever) "go run file.go
" follows the same logic, and is even
> less recommended.
>
> Both patterns are meant for very specific uses, and should be avoided
> in regular programs and packages.
>
> By: Carlos Castillo
So it turns out that Google Go enforces the import path for packages to be relative to the src
directory in the environmental variables GOROOT
or GOPATH
.
Thus here's the fix to make the program build with go build
.
Note: GOPATH
is set to C:\gopath
Change main.go
from
package main
import _ "./local_file"
func main() {
}
To this
package main
import _ "bug/local_file"
func main() {
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论