What makes go build fail with "unexpected input NUL"?

I have a Linux virtual machine where I am trying to compile a simple Go package. The package was uploaded to my user directory using git:

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

I installed GOPATH and built:

 $ 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 in front of the building will allow it to be built. But this is inconvenient because tools like github.com/codegangsta/gin that try to rebuild the package will not work.

+5
source share
1 answer

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 the main one and file names are specified, the file name comes from the first file name, for example, f1 for 'go build f1.go f2.go'; without files ('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 this:

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

During go build , if the output from the previous assembly, test.go , exists, it will be considered as the source file, causing the message "unexpected NUL in input".

The problem can be resolved by renaming the directory in order to avoid the release of an assembly that has a name that will be considered part of the package.

+3
source

Source: https://habr.com/ru/post/1208615/


All Articles