如果我在.go文件目录之外,go安装失败

i5desfxk  于 2023-06-19  发布在  Go
关注(0)|答案(2)|浏览(107)

我通过建议https://golang.org/doc/install安装了Golang,似乎我不能像在网站上那样运行go install命令
如果我从.go文件所在目录以外的任何目录运行该命令,则会出现此错误。例如:

go install ./src/tutorial/helloworld/hello.go

go install ./path/to/.go/file/hello.go

go install: no install location for .go files listed on command line (GOBIN not set)

但是如果我从包含.go文件的目录中运行安装,一切都很顺利,它将最终的可执行文件放置在GOPATHbin文件夹中。

//In the folder that contains my .go file
go install

我在.bash_profile中设置了GOPATH,go安装将/usr/local/go/bin中的go根目录添加到了PATH
为什么我不能像www.example.com网站上的说明那样从目录外运行installgolang.org?

wmvff8tz

wmvff8tz1#

go install命令的参数是包,而不是.go文件。使用这些命令按相对路径指定包。
对于包含./src/tutorial/helloworld/hello.go文件的包:

go install ./src/tutorial/helloworld

对于包含文件./path/to/.go/file/hello.go的包:

go install ./path/to/.go/file
qxsslcnc

qxsslcnc2#

我尝试了选择的答案解决方案,但不适合我,给了我以下错误
找不到主模块,但在'path/to/root/of/project'中找到了.git/config以在那里创建模块,运行:cd .. && go mod init
当我搜索了一下,我发现我需要在mod存在的文件夹中添加git init,我不知道我是否理解了解决方案,但我不想添加git init,因为我在根文件夹中有一个
上下文“我想在root-folder/dir1/中运行go install”
使用go help build
-C目录
在运行命令之前更改为dir。在命令行上命名的任何文件都将在更改目录后进行解释。所以对我有效的是

go install -C  ./path/to/.go/file

相关问题