英文:
Invalid Argument Running Google Go Binary in Linux
问题
我已经用Go语言编写了一个非常小的应用程序,并配置了一个AWS Linux AMI来托管。该应用程序是一个非常简单的Web服务器。我按照官方文档的说明,严格按照指示在Linux虚拟机上安装了Go。当我使用"go run main.go"命令调用我的应用程序时,它能够正常运行。
然而,当我尝试手动启动由"go install"命令生成的二进制文件时,我收到一个"Invalid argument"错误。相反,如果我运行"go build"(我理解这两个命令本质上是相同的,只是有一些例外),然后调用生成的二进制文件,应用程序会正常启动。
我是在"$GOPATH/bin/"文件夹中调用该文件的:
./myapp
我还将"$GOPATH/bin"添加到"$PATH"变量中。
我还将二进制文件从"$GOPATH/bin/"移动到"src"文件夹,并成功地从那里运行它。
Linux实例是一个64位实例,我已经安装了相应的64位Go安装包。
英文:
I’ve written a very small application in Go, and configured an AWS Linux AMI to host. The application is a very simple web server. I’ve installed Go on the Linux VM by following the instructions in the official documentation to the letter. My application runs as expected when invoked with the “go run main.go
” command.
However, I receive an “Invalid argument” error when I attempt to manually launch the binary file generated as a result of running “go install
”. Instead, if I run “go build
” (which I understand to be essentially the same thing, with a few exceptions) and then invoke the resulting binary, the application launches as expected.
I’m invoking the file from within the $GOPATH/bin/
folder as follows:
./myapp
I’ve also added $GOPATH/bin
to the $PATH
variable.
I have also moved the binary from $GOPATH/bin/
to the src
folder, and successfully run it from there.
The Linux instance is a 64-bit instance, and I have installed the corresponding Go 64-bit installation.
答案1
得分: 1
go build
命令会构建所有依赖的包,然后生成可执行文件,最后丢弃中间结果(参考这里获取另一种观点;还可以仔细阅读go help build
和go help install
的输出)。
相反,go install
命令会使用预编译的依赖包版本,如果找到的话;否则也会构建它们,并安装到$PATH/pkg
目录下。因此,我建议你在$GOPATH/src
目录下运行go install ./...
命令。
或者,对于每个依赖包,可以选择性地运行go install uri/of/the/package
命令,然后再尝试构建可执行文件。
英文:
go build
builds everything (that is, all dependent packages), then produces the resulting executable files and then discards the intermediate results (see this for an alternative take; also consider carefully reading outputs of go help build
and go help install
).
go install
, on the contrary, uses precompiled versions of the dependent packages, if it finds them; otherwise it builds them as well, and installs under $PATH/pkg
. Hence I might suggest that go install
sees some outdated packages which screw the resulting build.
Consider running go install ./...
in your $GOPATH/src
.
Or may be just selective go install uri/of/the/package
for each dependent package, and then retry building the executable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论