英文:
Go install doesn't create any bin file
问题
我的文件夹结构是正确的,我可以在包文件夹内部或系统的任何位置运行go install
命令,并在install
之后添加包(文件夹)名称。
例如,我的工作空间如下所示:
Go\
bin\
pkg\
src\
name\
file.go
然后,如果我运行以下命令:
cd %GOPATH%\src\name
go install
或者
go install name
不会生成任何错误,并且我的工作空间变成了以下形式:
Go\
bin\
pkg\
windows_amd64\ <-- 新增!
name.a <-- 新增!
src\
name\
file.go
包文件被正确创建,但是bin文件没有被创建。
我的go env如下所示:
C:\Users\...>go env
set GOARCH=amd64
set GOBIN=C:\Users\myname\Documents\Go\bin
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\myname\Documents\Go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
为什么会出现这种情况?我是否遗漏了什么,做错了什么?我希望能够同时创建bin文件和包文件。
英文:
My folder structure is correct, i can both run go install
from inside the package folder and from anywhere in the system, adding the package (folder) name after install
.
For example, my workspace is the following:
Go\
bin\
pkg\
src\
name\
file.go
then, if i run
cd %GOPATH%\src\name
go install
or
go install name
no errors are generated and my workspace becomes the following
Go\
bin\
pkg\
windows_amd64\ <-- new!
name.a <-- new!
src\
name\
file.go
Package files are correctly created, but bin files aren't.
My go env is the following:
C:\Users\...>go env
set GOARCH=amd64
set GOBIN=C:\Users\myname\Documents\Go\bin
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\myname\Documents\Go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
Why is this the case? Am i missing something, doing something wrong? I want bin files to be created along with package files.
答案1
得分: 58
一个可能的原因是file.go
不在package main
中。
例如,参见“你的第一个程序”。
如果是这样的话,它会在bin
中生成一个可执行文件。
文章“go build命令是如何工作的?”提到:
Go命令是一个名为
main
的包。
主包或命令与其他包一样进行编译,但随后还要经过几个额外的步骤来链接成最终的可执行文件。
英文:
One reason could be the file.go
isn't in package main
.
See for instance "Your first program"
If it was, that would generate a executable in bin
.
The article "How does the go build command work ?" does mention:
> A Go command is a package whose name is main
.
Main packages, or commands, are compiled just like other packages, but then undergo several additional steps to be linked into final executable.
答案2
得分: 4
我遇到了一个不同的问题,希望这对某人有所帮助。这并不是针对这个问题的具体回答,但至少对我来说解决了为什么没有生成二进制文件的问题。
假设你的目录结构如下:
.go/
bin/
pkg/
src/
github.com/
banool/
myapp/
file.go
server.go
cmd/
myapp/
main.go
我需要运行以下两个命令:
go build github.com/banool/myapp/...
go build github.com/banool/myapp/cmd/...
之后,我的二进制文件将出现在 myapp/
目录下。
显然,这与上面的答案是非常不同的情况,但我遇到了这样的情况,我运行了 go get
命令来获取一些 Go 源代码,但我不知道如何构建它。希望这对那些遇到这个问题的人有所帮助
英文:
I came to this question with a different problem so hopefully this helps someone. This isn't answering this question specifically, but it does answer for me at least why no binary file was being made.
Let's say your directory structure looks like this:
.go/
bin/
pkg/
src/
github.com/
banool/
myapp/
file.go
server.go
cmd/
myapp/
main.go
I had to run these two commands:
go build github.com/banool/myapp/...
go build github.com/banool/myapp/cmd/...
After this my binary appeared under myapp/
.
Obviously this is a very different situation to the answer above, but I came to this question with such a situation, where I had run go get
to get some Go source and I didn't know how to build it. Hopefully this helps people stumbling across this question with that problem
答案3
得分: 1
好的,以下是翻译好的内容:
作为第一个程序,我使用了第一行代码:
package hello
然后它在 pkg 文件夹下创建了 "hello.a" 文件。
当我将其改为 "package main" 时,可执行文件就生成了。
英文:
Well, as a first program, I used first line as
package hello
Then it was creating "hello.a" under pkg folder.
Moment I changed it to "package main", the executable was generated
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论