`go build` 失败并显示 `unexpected NUL in input` 错误的原因是什么?

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

What causes 'go build' to fail with 'unexpected NUL in input'?

问题

我有一个Linux虚拟机,我正在尝试编译一个简单的Go包。该包是通过git检出到我的用户目录中的:

$ git clone [...]/test.go
正在克隆到 'test.go'...
完成。
$ cd test.go/
$ ls
main.go

我设置了GOPATH并进行了构建:

$ export GOPATH=$PWD; echo $GOPATH
/home/vagrant/test.go
$ go build
$ ls
main.go test.go*

到目前为止一切顺利。但是现在当我尝试再次构建时,它失败了:

$ go build
无法加载包:package .: read /home/vagrant/test.go/test.go: unexpected NUL in input

在构建之前删除test.go文件将允许它构建。但这很不方便,因为像github.com/codegangsta/gin这样尝试重新构建包的工具将会失败。

英文:

I have a Linux VM where I am trying to compile a simple Go package. The package was retrieved into my user directory with git:

$ git clone [...]/test.go
Cloning into 'test.go'...
done.
$ cd test.go/
$ ls
main.go

I set up the GOPATH and build:

$ export GOPATH=$PWD; echo $GOPATH
/home/vagrant/test.go
$ go build
$ ls
main.go test.go*

So far so good. But now when I try to build again, it fails:

$ go build
can't load package: package .: read /home/vagrant/test.go/test.go: unexpected NUL in input

Deleting the test.go file before building will allow it to build. But this is inconvenient because tools like github.com/codegangsta/gin which try to rebuild the package will fail.

答案1

得分: 5

该存储库被命名为[...]/test.go,而git clone的默认容器目录是存储库名称,因此包含目录被命名为test.go\

根据go help build

如果包是main并且提供了文件名,则文件名派生自第一个提到的文件名,例如对于'go build f1.go f2.go',如果没有提供文件('go build'),输出文件名是包含目录的基本名称。

在这种情况下,输出文件名为test.go。问题是:

在包所在的目录中,.go、.c、.h和.s 文件被视为包的一部分

在进行go build时,如果先前构建的输出文件test.go存在,它将被视为源文件,从而触发“输入中的意外NUL”消息。

可以通过重命名目录来解决该问题,以避免构建输出具有被视为包的名称。

英文:

The repository was named [...]/test.go, and the default container directory for git clone is the repo name, so the containing directory is named test.go\.

From go help build:

> If the package is main and file names are provided, the file name
> derives from the first file name mentioned, such as f1 for 'go build
> f1.go f2.go'; with no files provided ('go build'), the output file
> name is the base name of the containing directory.

In this case the output is a file called test.go. The problem is:

> In the directory containing the package, .go, .c, .h, and .s files are considered part of the package

During a go build if the output from a previous build, test.go, exists, it will be treated as a source file, triggering the 'unexpected NUL in input' message.

The problem can be resolved by renaming the directory to avoid the build output having a name that will be considered part of the package.

huangapple
  • 本文由 发表于 2014年12月9日 08:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/27369324.html
匿名

发表评论

匿名网友

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

确定