go build vs go build file.go

huangapple go评论82阅读模式
英文:

go build vs go build file.go

问题

go buildgo 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:&gt;type main.go
package main

import _ "./local_file"

func main() {
}

C:\gopath\src\bug:&gt;type local_file\local_file.go
package local_file

import "fmt"

func init() {
		fmt.Println("Called: local_file.init()")
}

C:\gopath\src\bug:&gt;go run main.go
Called: local_file.init()

C:\gopath\src\bug:&gt;go build main.go

C:\gopath\src\bug:&gt;dir
...
<DIR>          local_file
		 1,285,120 main.exe
			55 main.go	
...

C:\gopath\src\bug:&gt;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&#39;t load package: C:\go\src\bug\main.go:3:8: local import &quot;./local_file&quot; 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:&gt;go version
go version go1.1 windows/386
C:\gopath\src\bug:&gt;dir
...
&lt;DIR&gt;          local_file
				55 main.go
...

C:\gopath\src\bug:&gt;type main.go
package main

import _ &quot;./local_file&quot;

func main() {
}

C:\gopath\src\bug:&gt;type local_file\local_file.go
package local_file

import &quot;fmt&quot;

func init() {
		fmt.Println(&quot;Called: local_file.init()&quot;)
}

C:\gopath\src\bug:&gt;go run main.go
Called: local_file.init()

C:\gopath\src\bug:&gt;go build main.go

C:\gopath\src\bug:&gt;dir
...
&lt;DIR&gt;          local_file
		 1,285,120 main.exe
				55 main.go	
...

C:\gopath\src\bug:&gt;go build
can&#39;t load package: C:\gopath\src\bug\main.go:3:8: local import &quot;./local_file&quot; in non-local package

答案1

得分: 9

我在golang的Google Group网站上提出了这个问题,这是其中一个回答:

go build file.go(即使file.goGOPATH中)并不会构建工作区中的一个包,所以它不遵循那个规则。缺点是你必须命名所有的*.go文件,你无法获得go命令的一些自动行为(例如:测试和安装),而且你无法构建一个需要安装的包,只能构建可执行文件。此外,它也无法通过go get获取。

go build file.go”只应该用于简单的单源文件二进制文件,你不打算分发或重用其中的代码。即使是这样,按照go工具的约定通常更简单更快:每个文件夹一个包(反之亦然),不使用相对导入(永远不要使用),“go run file.go”也遵循相同的逻辑,但更不推荐使用。

这两种模式都是为特定用途而设计的,应该避免在常规程序和包中使用。

作者:Carlos Castillo

所以事实证明,Google Go要求包的导入路径相对于环境变量GOROOTGOPATH中的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 _ &quot;./local_file&quot;

func main() {
}

To this

package main

import _ &quot;bug/local_file&quot;

func main() {
}

huangapple
  • 本文由 发表于 2013年10月8日 04:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/19234445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定