我想从github安装包到我的$GOPATH,我已经尝试过了:

go get github.com:capotej/groupcache-db-experiment.git

存储库在这里。


首先,我们需要GOPATH

$GOPATH是由其环境变量指定的文件夹(或文件夹集)。我们必须注意,这不是安装Go的$GOROOT目录。

export GOPATH=$HOME/gocode
export PATH=$PATH:$GOPATH/bin

我们在计算机中使用~/gocode路径来存储应用程序的源代码及其依赖项。GOPATH目录还将存储它们的包的二进制文件。

然后检查Go env

你的系统必须有$GOPATH和$GOROOT,下面是我的Env:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/elpsstu/gocode"
GORACE=""
GOROOT="/home/pravin/go"
GOTOOLDIR="/home/pravin/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

现在,你运行download go package:

go get [-d] [-f] [-fix] [-t] [-u] [build flags] [packages]

Get下载并安装按导入路径命名的包及其依赖项。欲了解更多细节,请点击这里。


Command go Download and install packages and dependencies Usage: go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [build flags] [packages] Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'. The -d flag instructs get to stop after downloading the packages; that is, it instructs get not to install the packages. The -f flag, valid only when -u is set, forces get -u not to verify that each package has been checked out from the source control repository implied by its import path. This can be useful if the source is a local fork of the original. The -fix flag instructs get to run the fix tool on the downloaded packages before resolving dependencies or building the code. The -insecure flag permits fetching from repositories and resolving custom domains using insecure schemes such as HTTP. Use with caution. The -t flag instructs get to also download the packages required to build the tests for the specified packages. The -u flag instructs get to use the network to update the named packages and their dependencies. By default, get uses the network to check out missing packages but does not use it to look for updates to existing packages. The -v flag enables verbose progress and debug output. Get also accepts build flags to control the installation. See 'go help build'. When checking out a new package, get creates the target directory GOPATH/src/. If the GOPATH contains multiple entries, get uses the first one. For more details see: 'go help gopath'. When checking out or updating a package, get looks for a branch or tag that matches the locally installed version of Go. The most important rule is that if the local installation is running version "go1", get searches for a branch or tag named "go1". If no such version exists it retrieves the default branch of the package. When go get checks out or updates a Git repository, it also updates any git submodules referenced by the repository. Get never checks out or updates code stored in vendor directories. For more about specifying packages, see 'go help packages'. For more about how 'go get' finds source code to download, see 'go help importpath'. This text describes the behavior of get when using GOPATH to manage source code and dependencies. If instead the go command is running in module-aware mode, the details of get's flags and effects change, as does 'go help get'. See 'go help modules' and 'go help module-get'. See also: go build, go install, go clean.


例如,显示详细输出,

$ go get -v github.com/capotej/groupcache-db-experiment/...
github.com/capotej/groupcache-db-experiment (download)
github.com/golang/groupcache (download)
github.com/golang/protobuf (download)
github.com/capotej/groupcache-db-experiment/api
github.com/capotej/groupcache-db-experiment/client
github.com/capotej/groupcache-db-experiment/slowdb
github.com/golang/groupcache/consistenthash
github.com/golang/protobuf/proto
github.com/golang/groupcache/lru
github.com/capotej/groupcache-db-experiment/dbserver
github.com/capotej/groupcache-db-experiment/cli
github.com/golang/groupcache/singleflight
github.com/golang/groupcache/groupcachepb
github.com/golang/groupcache
github.com/capotej/groupcache-db-experiment/frontend
$ 

注意,由于Go 1.17不支持使用Go get安装包:

不建议使用get构建和安装包。在未来的版本中,-d标志将默认启用,go get将仅用于调整当前模块的依赖关系。要使用来自当前模块的依赖项安装包,请使用go install。

这个“未来版本”是Go 1.18,正如发布说明中提到的:

Go get不再以模块感知模式构建或安装包。Go get现在致力于调整Go .mod中的依赖项。实际上,-d标志总是启用的。

(-d标志指示go get只下载软件包而不安装它们。)

使用go install代替:

# Install the latest version of a program,
# ignoring go.mod in the current directory (if any).
$ go install golang.org/x/tools/gopls@latest

# Install a specific version of a program.
$ go install golang.org/x/tools/gopls@v0.6.4

# Install a program at the version selected by the module in the current directory.
$ go install golang.org/x/tools/gopls

# Install all programs in a directory.
$ go install ./cmd/...

Go 1.18发布说明还提到Go get将像以前一样使用GO111MODULE=off。然而,在2022年,你肯定应该迁移到模块,并使用go install代替。


从控制台运行

go mod download