【问题标题】:GOPATH and GOBIN set and exported in Fedora but still not installing programGOPATH 和 GOBIN 在 Fedora 中设置并导出,但仍未安装程序
【发布时间】:2026-01-21 12:15:02
【问题描述】:

我在 Fedora 21 笔记本电脑上安装了 GO,并设置了 GOPATH 和 GOBIN,但由于某种原因它不允许我安装我的 go 程序。

pred@computer01 [20:03:02] ~  
$ echo $GOPATH
/home/pred/Documents/GO
pred@computer01 [20:03:11] ~  
$ echo $GOBIN
/home/pred/Documents/GO/bin
pred@computer01 [20:03:15] ~  
$ cd $GOPATH
pred@computer01 [20:03:21] ~/Documents/GO  
$ go install src/github.com/pred3/go_helloworld/helloworld/helloworld.go 
go install: no install location for .go files listed on command line (GOBIN not set)
pred@computer01 [20:03:32] ~/Documents/GO  
$ go env
GOARCH="amd64"
GOBIN="/home/pred/Documents/GO/bin"
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pred/Documents/GO"
GORACE=""
GOROOT="/usr/lib/golang"
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

我还应该做些什么才能让它发挥作用?

--编辑--

如下所述发出以下命令也会出错。

pred@computer1 [21:22:51] ~/Documents/GO  
$ go install src/github.com/pred3/go_helloworld/helloworld
can't load package: package src/github.com/predatorian3/go_helloworld/helloworld: cannot find package "src/github.com/pred3/go_helloworld/helloworld" in any of:
    /usr/lib/golang/src/src/github.com/pred3/go_helloworld/helloworld (from $GOROOT)
    /home/pred/Documents/GO/src/src/github.com/pred3/go_helloworld/helloworld (from $GOPATH)

但是,我尝试安装的 go 文件的开头没有 package main。一旦我将其更改为package main,它就起作用了。我不确定为什么我不能使用不同的包名。

【问题讨论】:

    标签: go fedora fedora-21


    【解决方案1】:

    go install 需要一个包作为参数(请参阅Description of package lists 以获得更详细的解释)。在你的情况下,它可能应该是

    go install github.com/pred3/go_helloworld/helloworld
    

    假设$GOPATH/src/github.com/pred3/go_helloworld/helloworld目录存在且$GOPATH/src/github.com/pred3/go_helloworld/helloworld/helloworld.gopackage main开头

    以下命令将执行相同的操作:

    cd $GOPATH/src/github.com/pred3/go_helloworld/helloworld
    go intsall
    

    【讨论】:

    • 更改包名有效。我无法使用自定义名称,但 package main 有效。出于某种原因,当我在 go install 命令中不包含主 go 文件时出现错误。我将在 OP 中发布错误。
    • 自定义包名称仅用于库(即其他库或命令使用的包)。应用程序(编译为二进制文件)应始终使用package main